0% found this document useful (0 votes)
58 views13 pages

HW2:Java Translator Program: Phuong Pham Pdpham@ucdavis - Edu

This document outlines the steps to build a Java program that can translate code written in the E language to C code. It describes 5 parts: 1) Using a scanner to tokenize input, 2) Building a parser to check syntax, 3) Implementing a symbol table to track variable scopes, 4) Generating C code output, and 5) Adding a for loop statement to the E language grammar. It provides TODO items and explanations for each part.

Uploaded by

Li Xio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views13 pages

HW2:Java Translator Program: Phuong Pham Pdpham@ucdavis - Edu

This document outlines the steps to build a Java program that can translate code written in the E language to C code. It describes 5 parts: 1) Using a scanner to tokenize input, 2) Building a parser to check syntax, 3) Implementing a symbol table to track variable scopes, 4) Generating C code output, and 5) Adding a for loop statement to the E language grammar. It provides TODO items and explanations for each part.

Uploaded by

Li Xio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

HW2:Java translator program

Phuong Pham pdpham@ucdavis.edu

NOTICE!!
Make sure your outputs are identical to that
of the *.correct files.

Debug with Eclipse


Copy the content of the test cases into
console panel
EOF = (Ctrl + D)

Part 1 - Scanner
Purpose of scanner
Break down the input string into tokens
In this part, Scanner prints out all tokens it finds

What are the tokens?


@,!,(,),+,-,*,/,[,:,|,%,],<,=,~,> and comma(,)
ids and numbers
...

Scanner: TODO
Read and understand Scan.scan() function
Provide code in Scan.java and TK.java for
omitted tokens:
e.g. =, ~, >

Part 2: Parser
Purpose of a parser:
Accept or reject given strings (recognizer)
Build parse tree
other operations, e.g. expression evaluation

The Parser in this part 2:


Verify if the given program is syntactically correct

Part 2: Parser - How does it work?


main() { program()}
program() {block()}
block() {
declare_list()
statement_list()
}
declare_list(){
while(there is a declaration)
{ declaration() }
}
declaration(){
must start with @
must be followed by an id
while (the next symbol is ,)
{must be followed by another id}
}

program ::= block


block ::= declaration_list statement_list
declaration_list ::= {declaration}
statement_list ::= {statement}
declaration ::= @ id { , id }

Look ahead for 1 token


If that token is @ then YES otherwise NO
Generally, check if lookahead token is in the
First_set of the non-terminal

Part 2:Parser - TODO


Translate the grammar into the system of
function calls
Identify the First_set for the non-terminals:
Use the First_set to identify the upcoming
non-terminal (repetition, alternatives)

Part 3:Symbol table


Scopes are delimited by blocks
block ::= declaration_list statement_list
declaration ::= @ id { , id }
program ::= block
guarded_command ::= expr : block
do ::= < guarded_command >
if ::= [ guarded_command { | guarded_command } [ % block ] ]

Example
@a
a = 999
!a+a
[a:!1111 % @a a=8888 !a]
!a

Semantics regarding scopes/variables:


Duplicate declaration
Usage of undeclared variable

Part 3:Symbol table - TODO


Implement a class for symbol tables that has
at least these functions

Add new symbol table(when entering a scope)


Remove the table(when exiting a scope)
Check if a variable is declared (at a specific scope)
Add a variable (to the table of the current scope)

Inject the appropriate function calls into


Parser

Part 3:Symbol table


Resizeable array in Java
java.util.ArrayList
java.util.Vector

ArrayList<ArrayList<Variable>>
class Variable{ public String name; public int
val;...}

Part 4: Generate code


Identify the non-terminals that need code
generation
Inject the output command to print
necessary C code to stdout
For example
private void program() {
System.out.print("#include <stdio.h>\n" +
"main()\n");
block();
}

Part 5: E Language Changes


Add definite iteration statement
(e.g. for loop) to Es grammar
Add rule(s) for this statment:
Specify starting condition, terminating condition, step
increase /decrease, operation in each iteration.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy