primary data structure for next phases of compilaqon

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r ID(party) Dan Grossman Dan Grossman Fall 2011 Fall 2011 We have: a scanner that generates a token stream SCOLON •  •  We want an abstract syntax tree (AST) –  A data structure that encodes the meaning of the program, and captures its structural features (loops, condiQonals, etc.) –  Primary data structure for next phases of compilaQon Winter 2013 UW CSE 401 (Michael Ringenburg) 6 How is this done? CSE341: Programming CSE341: Programming Languages Languages •  A grammar specifies the syntax of a language. Lecture 1 Lecture 1 •  Parsing aCourse Mechanics arse trees based on lgorithms bCourse Mechanics uild p a grammar Variabletream of tokens. ML Variable Bindings ML and a s Bindings –  Parse trees represent how a string can bDanerived e d Grossman from a grammar, and Fall encode meaning. 2011 •  –  E.g., add a and b, then add result to c. Expr à༎ Expr O Dan Grossmanp ID | ID F à༎ + Op all 2011 |- Can build AST by traversing parse tree (parsers may do this implicitly). Winter 2013 a+b+c Expr Expr ID(a) Expr Op ID(c) Op ID(b) + + UW CSE 401 (Michael Ringenburg) 7 DerivaQons vs. Parsing CSE341: Programming CSE341: Programming Languages Languages •  DerivaQon: a sequence of expansion steps, Lecture 1 Lecture 1 beginning with a start symbol and leading to a Course Course Mechanics Mechanics sequence of terminals Variable Bindings ML ML Variable Bindings •  Parsing: inverse of derivaQon Dan Grossman Dan Grossman –  Given a sequence of terminals (a.k.a. tokens) want Fall 2011 Fall 2011 to recover the nonterminals and structure (i.e., given string of terminals, find a derivaQon that generates them) •  Can represent derivaQon as a parse tree Winter 2013 UW CSE 401 (Michael Ringenburg) 8 Example DerivaQon C | program tatement CpSE341:statementSE341: sProgramming Languages rogram ::= Programming Languages G statement ::= assignStmt | ifStmt Lecture 1 assignStmt ::= id = expr ; ifStmt ::= if ( expr ) statement Course Mechanics expr ::= id | int | expr + expr id ::= a | b | c | i | j | k | n | x ML zVariable Bindings |y| int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online