Lab – Parsing & Abstract Syntax
Progress with Jlex?
Using CUP to construct Parser
Syntactic and Semantic Analysis
Abstract Syntax Trees
Adding semantic actions to Parser
 The actions are to construct an abstract syntax tree

Building parser
Need to write a grammar specification
Grm.cup  - on your handout
Look over PLDI3 notes for grammar specifications
Structure of this file
explained in book and on my web page in documentation

Syntactic vs Semantic analysis
Syntax
concerned with the structure of a program
consider at both Lexical level
stream of characters into tokens
and Phrase level
stream of tokens into phrases
Semantic
concerned with the meaning of a program
Sets of rules to cover both areas

Engineering Syntax and Semantic checks
Recursive descent, single-pass
muddle all of these operations together
lexical, syntax, semantic analysis
code generation
Multiple passes here
how do we transfer information between passes?  What do we need?

ASTs – Abstract Syntax Trees
Consider
( a := 5 ; a + 1 )

Using semantic actions in Parser
Series of classes to represent ASTs
Absyn directory
Use classes in action part of productions

For the lab
Thoroughly understand the handout
in conjunction with book and web
Download materials in advance
this time I WILL mail you when it's done

Examine Parse/Grm.cup
Notice
Structure of a production
Naming of items in the production
The action – constructing abstract syntax
Referring to the items
What is kleft?
Definitions of AST classes held in directory Absyn

To do:
Add a new rule for function declarations
Read Tiger spec to be sure what a function should look like
Follow style used for TypeDec since they’re similar
Call the rule FunDec and add other rules as necessary
Write new AST class – Absyn.FunctionDec

"Add rule/production for function..."
Add rule/production for function invocation
Where should it go?
Write a new AST class Absyn.CallExp
Reuse other ASTs and productions where possible
Questions for the next session
What is sym all about?
Why is –expect 3 required when building the parser?
Can we avoid it somehow?
Do we need to avoid it?
[Look at CUP pages and at chapter 3]