Lab – Parsing & Abstract Syntax
| Using CUP to construct Parser with semantic actions | ||
| The actions are to construct an abstract syntax tree | ||
| Setting a number of tasks | ||
| Note – these slides are available on the web | ||
| In your Tiger directory, copy over new files | ||
| cp -r $TIGER/Tiger/chap4/Absyn . | ||
| cp -r $TIGER/Tiger/chap4/Symbol . | ||
| cp -r $TIGER/Tiger/chap4/Parse/* Parse | ||
| Use CUP to build the parser | ||
| cd Parse | ||
| java java_cup.Main –expect 3 < Grm.cup | ||
| cd .. | ||
| then compile all .java files in Symbol, Absyn and Parse | ||
| Using your favourite Tiger program (no functions), to see the ASTs produced | ||
| java Parse.Main <program> | ||
| 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 | |||
| Change a simple bit of the grammar (keywords or ordering for example) and rebuild/test | |||
| Try parsing a program with an if..then..else.. | ||
| fix Grm.cup if there's a problem with it | ||
| 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 | ||
| Use existing AST class – Absyn.FunctionDec | ||
"Add rule/production for function..."
| Add rule/production for function invocation | |||
| Where should it go? | |||
| Use existing 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] | |||