Syntax Analysis. Tokens ---> Parse Tree. Main Problems. Grammars. Convert the list of tokens into a parse tree ( hierarchical analysis)

Similar documents
Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Context-free grammars


Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Compiler Construction: Parsing

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

LR Parsing Techniques

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

4. Lexical and Syntax Analysis

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

CPS 506 Comparative Programming Languages. Syntax Specification

Concepts Introduced in Chapter 4

Compilers. Bottom-up Parsing. (original slides by Sam

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

4. Lexical and Syntax Analysis

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

UNIT-III BOTTOM-UP PARSING

Chapter 4. Lexical and Syntax Analysis

VIVA QUESTIONS WITH ANSWERS

3. Parsing. Oscar Nierstrasz

Compiler Construction 2016/2017 Syntax Analysis

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Principles of Programming Languages COMP251: Syntax and Grammars

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Downloaded from Page 1. LR Parsing

A programming language requires two major definitions A simple one pass compiler

S Y N T A X A N A L Y S I S LR

CS 314 Principles of Programming Languages

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

LR Parsing LALR Parser Generators

Bottom-Up Parsing. Lecture 11-12

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Wednesday, August 31, Parsers

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Monday, September 13, Parsers

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Syntax Analysis Check syntax and construct abstract syntax tree

Topic 5: Syntax Analysis III

ML-Yacc User s Manual Version 2.4

Compiler Design Concepts. Syntax Analysis

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Bottom-Up Parsing. Lecture 11-12

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

EECS 6083 Intro to Parsing Context Free Grammars

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Topic 3: Syntax Analysis I

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

LR Parsing LALR Parser Generators

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Chapter 4: LR Parsing

Introduction to Parsing

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Context-free grammars (CFG s)

Context-Free Grammars

CMSC 330: Organization of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

Programming Language Syntax and Analysis

COP 3402 Systems Software Syntax Analysis (Parser)

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

CS 406/534 Compiler Construction Parsing Part I

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

LR Parsing Techniques

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

UNIT III & IV. Bottom up parsing

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Defining syntax using CFGs

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Parsing II Top-down parsing. Comp 412

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Syntax Analysis Part I

In One Slide. Outline. LR Parsing. Table Construction

Lecture Notes on Bottom-Up LR Parsing

Bottom-Up Parsing II. Lecture 8

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Lexical and Syntax Analysis. Bottom-Up Parsing

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Transcription:

Syntax Analysis Convert the list of tokens into a parse tree ( hierarchical analysis) source program lexical analyzer token get next token parser parse tree The syntactic structure is specified using context-free grammars [in lexical anlaysis, the lexical structure is specified using regular expressions] A parse tree (also called concrete syntax) is a graphic representation of a derivation that shows the hierarchical structure of the language Other secondary tasks: syntax error detection and recovery abstract syntax Tokens: FUNCTION (do_nothing1) LPARN (a) COLON (int) COMMA (b) COLON (string) RPARN Q (do_nothing2) LPARN INT(1) PLUS (a) RPARN Tokens ---> Parse Tree FUNCTION tyf ParseTree: COMMA COLON tyf LPARN COLON The parse tree captures the syntactic structure! fundec tyfields LPARN exp INT RPARN expl exp PLUS source program : function do_nothing1(a:int,b:string) = do_nothing2(1+a) Q exp RPARN exp Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 1 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 2 of 36 Main Problems How to specify the syntactic structure of a programming language? by using Context-Free Grammars (CFG)! How to parse? i.e., given a CFG and a stream of tokens, how to build its parse tree? 1. bottom-up parsing 2. top-down parsing How to make sure that the parser generates a unique parse tree? (the ambiguity problem) Given a CFG, how to build its parser quickly? using YACC ---- the parser generator How to detect, report, and recover syntax errors? Grammars A grammar is a precise, understandable specification of programming language syntax (but not semantics!) Grammar is normally specified using Backus-Naur Form (BNF) --- 1. a set of rewriting rules (also called productions) stmt -> if expr then stmt else stmt expr -> expr + expr expr * expr ( expr ) or 2. a set of non-terminals and a set of terminals non-terminals ---- terminals ---- 3. lists are specified using recursion stmt -> begin stmt-list end stmt-list -> stmt stmt ; stmt-list stmt, expr if, then, else, +, *, (, ), Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 3 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 4 of 36

Context-Free Grammars (CFG) A context-free grammar is defined by the following (T,N,P,S): T is vocabulary of terminals, N is set of non-terminals, P is set of productions (rewriting rules), and S is the start symbol (also belong to N). xample: a context-free grammar G=(T,N,P,S) T = { +, *, (, ), }, N = { }, P = { -> +, -> *, -> ( ), -> }, S = Written in BNF: -> + * ( ) All regular expressions can also be described using CFG Context-Free Languages (CFL) ach context-free gammar G=(T,N,P,S) defines a context-free language L = L(G) The CFL L(G) contains all sentences of teminal symbols (from T) --- derived by repeated application of productions in P, beginning at the start symbol S. xample the above CFG denotes the language L = L({ +, *, (, ), }, { }, { -> +, -> *, -> ( ), -> }, ) it contains sentences such as +, +(*), (), ***,... very regular language must also be a CFG! (the reverse is not true) Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 5 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 6 of 36 Derivations derivation is repeated application of productions to yield a sentence from the start symbol: the intermediate forms always contain some non-terminal symbols leftmost derivation : at each step, leftmost non-terminal is replaced; e.g. => * => * => * rightmost derivation : at each step, rightmost non-terminal is replaced; e.g. => * => * => * () => * ( + ) => * ( + ) => * ( + ) => * => * => * --- derives * --- derives --- derives () Summary: => * * ( + ) => * : derives in 0 or more steps Parse Tree A parse tree is a graphical representation of a derivation that shows hierarchical structure of the language, independent of derivation order. Parse trees have leaves labeled with terminals; interior nodes labeled with non-terminals. example: =>* * ( + ) * ( ) + very parse tree has unique leftmost (or rightmost) derivation! Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 7 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 8 of 36

Ambiguity A language is ambiguous if a sentence has more than one parse tree, i.e., more than one leftmost (or rightmost) derivation example: + * another leftmost derivation: Resolving Ambiguity Solution #1 : using disambiguating rules such as precedence... e.g. let * has higher priority over + (favor derivation (a)) a) => + => + => + * => + * => + * b) => * => + * => + * => + * => + * Solution #2 : rewriting grammar to be unambiguous! dangling-else stmt -> if expr then stmt if expr then stmt else stmt. + * How to parse the following? * + if 1 then if 2 then S1 else S2 How to rewrite? Main Idea: build precedence into grammar with extra non-terminals! Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 9 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 10 of 36 Resolving Ambiguity (cont d) solution: define matched and unmatched statements stmt -> m-stmt um-stmt m-stmt -> if expr then m-stmt else m-stmt. um-stmt -> if expr then stmt if expr then m-stmt else um-stmt Now how to parse the following? if 1 then if 2 then S1 else S2 Resolving Ambiguity (cont d) Another ambiguous grammar -> + - * / ( ) - usual precedence: highest - (unary minus) * / lowest + - Build grammar from highest ---> lowest precendence element -> ( expr ) primary -> - primary element term -> term * primary term / primary primary expr -> expr + term expr - term term try the leftmost derivation for - + * expr => expr + term => term + term => primary + term => - primary + term => - element + term => - + term => - + term * primary =>... =>* - + * Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 11 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 12 of 36

Other Grammar Transformations limination of Left Recursion (useful for top-down parsing only) replace productions of the form with A -> A x y A -> y A A -> x A ε (yields different parse trees but same language) see Appel pp 51-52 for the general algorithm Left Factoring --- find out the common prefixes (see Appel pp 53) change the production to A -> x y x z A -> x A A -> y z Parsing parser : a program that, given a sentence, reconstructs a derivation for that sentence ---- if done sucessfully, it recognizes the sentence all parsers read their input left-to-right, but construct parse tree differently. bottom-up parsers --- construct the tree from leaves to root shift-reduce, LR, SLR, LALR, operator precedence top-down parsers --- construct the tree from root to leaves recursive descent, predictive parsing, LL(1) parser generator --- given BNF for grammar, produce parser YACC --- a LALR(1) parser generator Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 13 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 14 of 36 Top-Down Parsing Construct parse tree by starting at the start symbol and guessing at derivation step. It often uses next input symbol to gue guessing. example: S c S -> c A d A -> ab a S A dece which rule of A to use here? Main algorithms : recursive descent, predictive parsing (see the textbook for detail) d c a S A input symbols: b d dece to use 1st alternative of A c cad S A a d guessed wrong backtrack, and try 2nd one. Bottom-Up Parsing Construct parse tree bottom-up --- from leaves to the root Bottom-up parsing always constructs right-most derivation Important parsing algorithms: shift-reduce, LR parsing,... shift-reduce parsing : given input string w, reduces it to the start symbol! Main ea: look for substrings that match r.h.s of a production xample: sentential form reduction Grammar abbcde aabcde A -> b right-most S -> aacbe aacde A -> Ab derivation A -> Ab b in reverse aacbe B -> d B -> d S S -> aacbe Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 15 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 16 of 36

Handles Handles are substrings that can be replaced by l.h.s. of productions to lead to the start symbol. Not all possible replacements are handles --- some may not lead to the start symbol... abbcde -> aabcde -> aaacde -> stuck! this b is not a handle! Definition : if γ can be derived from S via right-most derivation, then γ is called a right-sentential form of the grammar G (with S as the start symbol). Similar definiton for left-sentential form. handle of a right-sentential form γ = αaω is A -> β if S => * rm α A ω => rm α β ω and ω contains only terminals..g., A -> Ab in aabcde Handle Pruning Main ea: start with terminal string w and prune handles by replacing them with l.h.s. of productions until we reach S : S => rm γ 1 => rm γ 2 => rm.. => rm γ n-1 => rm ω xample: ambiguity (i.e., construct the rightmost derivation in reverse) -> + * ( ) a b c right-sentential form handle reducing production a + b * c a -> a + b * c b -> b + * c c -> c + * * -> * + + -> + Key of Bottom-Up Parsing: Identifying Handles Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 17 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 18 of 36 Shift-Reduce Parsing Using a stack, shift input symbols onto the stack until a handle is found; reduce handle by replacing grammar symbols by l.h.s. of productions; accept for successful completion of parsing; error for syntax errors. xample: -> + * ( ) a b c stack input action $ a+b*c$ shift $a +b*c$ reduce: -> a $ +b*c$ shift $+ b*c$ shift $+b *c$ reduce: -> b $+ *c$ shift (possible SR conflict) $+* c$ shift $+*c $ reduce: -> c $+* $ reduce: -> * $+ $ reduce: -> + $ $ accept handle is always at the top! Conflicts ambiguous grammars lead to parsing conflicts; conflicts can be fixed by rewriting the grammar, or making a decision during parsing shift / reduce (SR) conflicts : choose between reduce and shift actions S -> if then S if then S else S. stack input action $if then S else...$ reduce or shift? reduce/reduce (RR) conflicts : choose between two reductions stmt -> (param) --- procedure call a(i) param -> -> () --- array subscript a(i) stack input action $( )...$ reduce to or param? Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 19 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 20 of 36

LR Parsing today s most commonly-used parsing techniques! LR(k) parsing : the L is for left-to-right scanning of the input; the R for constructing a rightmost derivation in reverse, and the k for the number of input symbols of lookahead used in making parsing decisions. (k=1) LR parser components: input, stack (strings of grammar symbols and states), driver routine, parsing tables. stack s m X m.. s 1 X 1 s 0 input: a 1 a 2 a 3 a 4.a n $ LR Parsing Program Parsing Table(action+goto) output LR Parsing (cont d) A sequence of new state symbols s 0, s 1, s 2,..., s m ----- each state sumarizes the information contained in the stack below it. Parsing configurations: (stack, remaining input) written as (s 0 X 1 s 1 X 2 s 2...X m s m, a i a i+1 a i+2...a n $) next move is determined by Parsing tables: Table A s m and a i ACTION[s,a] and GOTO[s,X] ACTION[s,a] --- s : state, a : terminal itsentries (1) shift s k (2) reduce A -> β (3) accept (4) error Table G GOTO[s,X] --- s : state, X : non-terminal its entries are states Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 21 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 22 of 36 LR Parsing Driver Routine Given the configuration: (s 0 X 1 s 1 X 2 s 2...X m s m, a i a i+1 a i+2...a n $) (1) If ACTION[s m,a i ] is shift s, enter config (s 0 X 1 s 1 X 2 s 2...X m s m a i s, a i+1 a i+2...a n $) (2) If ACTION[s m,a i ] is reduce A->β, enter config (s 0 X 1 s 1 X 2 s 2...X m-r s m-r As, a i a i+1 a i+2...a n $) where r= β, and s = GOTO[s m-r,a] (here β should be X m-r+1 X m-r+2...x m ) (3) If ACTION[s m,a i ] is accept, parsing completes (4) If ACTION[s m,a i ] is error, attempts error recovery. Grammar: xample: LR Parsing 1. S -> S ; S 6. -> + 2. S -> := 7. -> (S, ) 3. S -> print (L) 8. L -> 4. -> 9. L -> L, 5. -> num Tables : sn -- shift and put state n on the stack gn -- go to state n rk -- reduce by rule k a -- accept and parsing completes _ -- error Details see figure 3.18 and 3.19 in Appel pp.56-57 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 23 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 24 of 36

Summary: LR Parsing LR Parsing is doing reverse right-most derivation!!! If a grammar is ambiguous, some entries in its parsing table (ACTION) contain multiple actions : shift-reduce or reduce-reduce conflicts. Yacc Yacc is a program generator ---------- it takes grammar specification as input, and produces an LALR(1) parser written in C. Two ways to resolve conflicts ---- (1) rewrite the grammar to be unambiguous (2) making a decision in the parsing table (retaining only one action!) Grammar Specification foo.y Yacc y.tab.c LR(k) parsing: parsing moves determined by state and next k input y.tab.c C Compiler a.out symbols; k = 0, 1 are most common. A grammar is an LR(k) grammar, if each entry in its LR(k)-parsing table is uniquely defined. How to build LR parsing table? ---- three famous varieties: SLR, LR(1), LALR(1) (detailed algorithms will be taught later!) input text a.out parse tree,. Implementation of Yacc: Construct the LALR(1) parser table from the grammar specification Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 25 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 26 of 36 ML-Yacc ML-Yacc is like Yacc ---------- it takes grammar specification as input, and produces a LALR(1) parser written in Standard ML. Grammar Specification foo.grm foo.lex.sml foo.grm.yacc ML-Yacc ML Compiler foo.grm.yacc (in ML) Parser input text Parser parse tree. Implementation of ML-Yacc is similar to implementation of Yacc ML-Yacc Specification structure A = Absyn %term OF of string... %nonterm exp program... %pos int %eop OF %noshift OF grammar (action) program : exp () exp : () grammar is specified as BNF production rules; action is a piece of ML program; when a grammar poduction rule is reduced during the parsing process, the corresponding action is executed. user s ML declarations Yacc declarations rule-lists can call the above ML declarations Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 27 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 28 of 36

ML-Yacc Rules BNF production A -> α β... γ is written as A : α (action for A-> α) β (action for A-> β)... γ (action for A-> ρ) The start symbol is l.h.s. of the first production or symbol S in the Yacc declaration %start S The terminals or tokens are defined by the Yacc declaration %term %term of string NUM of int PLUS OF... The non-terminals are defined by the Yacc declaration %nonterm %nonterm XP of int START of int option xample: calc.grm fun lookup bogus = 10000 lookup s = 0 %eop OF SMI %pos int %left SUB PLUS %left TIMS DIV %term of string NUM of int PLUS TIMS PRINT SMI OF DIV SUB %nonterm XP of int START of int %verbose %name Calc START : PRINT XP (print XP; print \n ; XP) XP (XP) XP : NUM (NUM) (lookup ) XP PLUS XP (XP1+XP2) XP TIMS XP (XP1*XP2) XP DIV XP (XP1 div XP2) XP SUB XP (XP1-XP2) Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 29 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 30 of 36 Yacc : Conflicts Yacc uses the LR parsing (i.e. LALR); if the grammar is ambiguous, the resulting parser table ACTION will contain shift-reduce or reducereduce conflicts. In Yacc, you resolve conflicts by (1) rewriting the grammar to be unambiguous (2) declaring precendence and associativity for terminals and rules. Conser the following grammar and input PLUS PLUS : PLUS () TIMS () () we can specify TIMS has higher precedence than PLUS; and also assume both TIMS and PLUS are left associative. (also read the exampes on Appel pp73-74) Precedence and Associativity To resolve conflicts in Yacc, you can define precedence and associativity for each terminal. The precedence of each grammar rule is the precedence of its rightmost terminal in r.h.s of the rule. On shift / reduce conflict: if input terminal prec. > rule prec. then shift if input terminal prec. < rule prec. then reduce if input terminal prec. == rule prec. then { if terminal assoc. == left then reduce if terminal assoc. == right then shift if terminal assoc. == none then report error } if the input terminal or the rule has no prec. then shift & report error On reduce / reduce conflict: report error & rule listed first is chosen Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 31 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 32 of 36

Defining Prec. and Assoc. Defining precedence and associativity for terminals %left OR lowest prec. %left AND %noassoc Q NQ GT LT G L %left PLUS MINUS %left TIMS DIV highest prec. Defining precedence for rules using. %left PLUS MINUS %left TIMS DIV %left UNARYMINUS Must define UNARYMINUS as a new terminal! %prec xp : xp MINUS xp () xp TIMS exp () MINUS exp %prec UNARYMINUS (). Assuming unary minus has higher precedence than PLUS, MINUS, TIMS, DIV Only specifies the prec. of this rule == prec. of UNARY-MINUS Parser Description (.desc file) The Yacc declaration %verbose will produce a verbose description of the generated parser (i.e., the.desc file) 1. A summary of errors found while generating the parser 2. A detailed description of all errors 3. The parsing engine --- describing the states and the parser table (see xample 3.1 on pp15-18 in Appel s book) state 0: program :. exp shift 13 table ACTION INT shift 12 STRING shift 11 LPARN shift 10 MINUS shift 9 IF shift 8 program goto 135 exp goto 2 lvalue goto 1. error current states (characterized by grammar rules) table GOTO Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 33 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 34 of 36 Tiger.Lex File mumbo-jumbo You have to modify your tiger.lex file in assignment 2 by adding the following --- in order to generate the functor TigerLexFun type svalue = Tokens.svalue type pos = int type ( a, b) token = ( a, b) Tokens.token type lexresult = (svalue,pos) token %header (functor TigerLexFun(structure Tokens : Tiger_TOKNS));... Connecting Yacc and Lex signature PARS = sig val parse : string -> unit end structure Parse : PARS = struct structure TigerLrVals = TigerLrValsFun(structure Token = LrParser.Token) structure Lex = ToyLexFun(structure Tokens = TigerLrVals.Tokens) structure TigerP = Join(structure ParserData = TigerLrVals.ParserData structure Lex=Lex structure LrParser = LrParser) fun parse filename = let val _ = (rrormsg.reset(); rrormsg.filename := filename) val file = open_in filename fun parseerror(s,p1,p2) = rrormsg.error p1 s val lexer = LrParser.Stream.streamify (Lex.makeLexer (fn _ => TextIO.input file)) val (absyn, _) = TigerP.parse(30,lexer,parseerror,()) in close_in file; absyn end handle LrParser.Parserror => raise rrormsg.rror end Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 35 of 36 Copyright 1994-2007 Zhong Shao, Yale University Syntax Analysis : Page 36 of 36