You are on page 1of 3

Reflection

Feb 24
Ann Tung
913019827

Lexical Analysis: source code examined, tokens generated.

Parsing: build Abstract Syntax Tree to represent the program for correct grammar syntax for
subsequence processing

Grammar: strings with valid language syntax: G = ( N, T, P, S )
N: Nonterminal symbols which is disjoint with strings (upper case)
T: Terminal symbols (lower case)
P: Production (rewriting) rules
S: Start symbol

Nonterminals: Main structure components of the language: blocks, programs, expressions,
Terminals: Tokens used by the programmer and formed by the lexer
Production rules: The assignment of the nonterminals ( LHS -> RHS )
Start symbol: a nonterminal used to start the derivation / rewriting process
Continuenthe rewriting process as long as nonterminals exist

D = declarations
S = statements
* = zero or more
? = zero or one
E = expression
SE = simple expression
T = term
F = factor

FUNHEAD = function head separated by comma

Identifier -> (start with) letter then character*
Character -> letter | _ | … | $
Letter - > a | b | c | … | z
Decimal_literal -> [ 1 | 2 | … | 9 ] ( 0 | … | 9 )*
<id> valid identifier
<int> any integer

Parser: one procedure for each nonterminal to process the RHS represented by AST subtree.
The procedure for “program” build AST for the entire program

PROGRAM -> ‘program’ BLOCK : build a program tree with one child for the BLOCK tree
BLOCK -> ‘{‘ D* S* ‘}’ : build a block tree with children for declarations and statements
D -> TYPE NAME FUNHEAD BLOCK : build a functionDecl tree with four children:
type, name, function formals, body.
FUNHEAD -> ‘(‘ (D list ‘,’)? ‘)’ : one tree per formal declaration (D list ‘,’) separated by commas
F -> NAME ‘(‘ (E list ‘,’)? ‘)’ : one child for the function name and one child for each expression E

Compiler:
Lexer: scan source code to tokens
Lexer.setup: generate classes Symboland TokenType for lexer
Parser: analyze tokens, check syntax, build AST
Ast: abstract syntax tree classes
Visitor: walking the ast for printing, constraining, generating code
Compiler: compiler main program
Constrain: check type constraints; decorate ast with variable references to their declaration
Codegen: visit the decorated ast and generate bytecode
Interpreter: execute byte codes generated for the source program

Java utilities:
ArrayList: resizable array for List interface
Iterator interface: allows caller to remove elements from the underlying collection (ASTVisitors)
EnumSet: enumerated types (Token)

Class AST
Kids: ArrayList<AST>
nodeNum: int
decoration: AST
label: String
+getKid( i: int ): AST
+kidCount(): int
+getKids(): ArrayList<AST>
+addKid( kid: AST ): AST

AST packages (in subclasses)
AST (abstract)
ProgramTree
IdTree
RelOpTree

Class Parser
-currentToken: Token
-lex: Lexer
-relationalOps: EnumSet<Tokens>
-addingOps: EnumSet<Tokens>
-multiplyingOps: EnumSet<Tokens>
+Parser( sourceProgram: String ) - creates an instance of parser
+getLex(): Lexer
-scan() - scan nonterminal, check syntax, advance to just after the phrase.
+execute(): AST - build AST ProgramTree via rProgram
+rProgram(): AST - check program token, call rBlock add BlockTree as child of ProgramTree
-expect( kind: Tokens ) - check and trhow SyntaxError exception
-isNextTok( kind: Tokens ): boolean
+rBlock(): AST - check {, D* (add Decl AST via rDecl), S* (add statement AST via rStatement), }
startingDecl(): Boolean
startingstatmenet(): Boolean
+rDecl(): AST - DeclTree (first production) or FunctionDeclTree (second production)
+rType(): AST - child to FunctionDeclTree(), update since new types added
+rName(): AST - child to rType()
+rFunHead: AST - list of decl’s in parenthesis separated by commas
+rSimpleExpr(): AST – order matters, picks as many T’s (left associative, left-to-right order)
-getAddOperTree(): AST
+rStatement(): AST
+rExpr(): AST
+rTerm(): AST
+rFactor(): AST
getRelationTree(): AST
-getMultOperTree(): AST
class SyntaxError
-tokenFound: Token
kindExpected: Tokens
+SyntaxError( tokenFound: Token, kindExpected: Tokens )
print()


Associativity:
left-associative: +, -, *, /
right-associative: ^, = (right of = is evaluated)

grammar: structure of all the phrases (phrase structure grammar)
nonterminal: structure of a set of phrases (S: statement, program: all program)
statement: make “if” higher priority

You might also like