Вы находитесь на странице: 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.

Вам также может понравиться