Lex and Yacc. More Details

Similar documents
Lex and Yacc. A Quick Tour

HW8 Use Lex/Yacc to Turn this: Into this: Lex and Yacc. Lex / Yacc History. A Quick Tour. if myvar == 6.02e23**2 then f(..!

Using an LALR(1) Parser Generator

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

Context-free grammars

Syntax Analysis The Parser Generator (BYacc/J)

Compiler Construction: Parsing

Syntax Analysis Part IV

Ray Pereda Unicon Technical Report UTR-03. February 25, Abstract

Introduction to Yacc. General Description Input file Output files Parsing conflicts Pseudovariables Examples. Principles of Compilers - 16/03/2006

Lexical and Syntax Analysis

Syntax-Directed Translation

TDDD55 - Compilers and Interpreters Lesson 3

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

CS 4120 Introduction to Compilers

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

Bottom-Up Parsing. Lecture 11-12

Lex & Yacc (GNU distribution - flex & bison) Jeonghwan Park

Gechstudentszone.wordpress.com

Yacc: A Syntactic Analysers Generator

PRACTICAL CLASS: Flex & Bison

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

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

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Compiler Construction

Compiler Construction

Principles of Programming Languages

LECTURE 7. Lex and Intro to Parsing

Last Time. What do we want? When do we want it? An AST. Now!

An Introduction to LEX and YACC. SYSC Programming Languages

Introduction to Lex & Yacc. (flex & bison)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

UNIT III & IV. Bottom up parsing

Chapter 4. Lexical and Syntax Analysis

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

4. Lexical and Syntax Analysis

LECTURE 11. Semantic Analysis and Yacc

Parser Tools: lex and yacc-style Parsing

I 1 : {E E, E E +E, E E E}

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

Yacc Yet Another Compiler Compiler

Bottom-Up Parsing. Lecture 11-12

4. Lexical and Syntax Analysis

shift-reduce parsing

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

LR Parsing LALR Parser Generators

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

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

How do LL(1) Parsers Build Syntax Trees?

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Parser Tools: lex and yacc-style Parsing

Classes Basic Overview

Input from Files. Buffered Reader

Compiler Design 1. Yacc/Bison. Goutam Biswas. Lect 8

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

Lecture 8: Deterministic Bottom-Up Parsing

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

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Lex & Yacc. By H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Lex & Yacc. by H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Lecture 7: Deterministic Bottom-Up Parsing

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Syntactic Analysis. Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree.

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

Parser and syntax analyzer. Context-Free Grammar Definition. Scanning and parsing. How bottom-up parsing works: Shift/Reduce tecnique.

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Compiler Passes. Syntactic Analysis. Context-free Grammars. Syntactic Analysis / Parsing. EBNF Syntax of initial MiniJava.

The source code is as follows:

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

LR Parsing LALR Parser Generators


Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Abstract Syntax. Mooly Sagiv. html://

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

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CS415 Compilers. LR Parsing & Error Recovery

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Defining syntax using CFGs

Top down vs. bottom up parsing

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

CS 314 Principles of Programming Languages

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

CSCI Compiler Design

More Assigned Reading and Exercises on Syntax (for Exam 2)

Operators in java Operator operands.

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

LL(1) predictive parsing

CIT 3136 Lecture 7. Top-Down Parsing

TDDD55- Compilers and Interpreters Lesson 3

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Configuration Sets for CSX- Lite. Parser Action Table

Compiler Lab. Introduction to tools Lex and Yacc

Transcription:

Lex and Yacc More Details

Calculator example From http://byaccj.sourceforge.net/ %{ import java.lang.math; import java.io.*; import java.util.stringtokenizer; %} /* YACC Declarations; mainly op prec & assoc */ %token NUM %left '-' '+ %left '*' '/ %left NEG /* negation--unary minus */ %right '^' /* exponentiation */ /* Grammar follows */ %%...

... /* Grammar follows */ %% input: /* empty string */ input line ; input is one expression per line; output is its value line: \n exp \n { System.out.println(" + $1.dval + " "); } ; exp: NUM { $$ = $1; } exp '+' exp { $$ = new ParserVal($1.dval + $3.dval); } exp '-' exp { $$ = new ParserVal($1.dval - $3.dval); } exp '*' exp { $$ = new ParserVal($1.dval * $3.dval); } exp '/' exp { $$ = new ParserVal($1.dval / $3.dval); } '-' exp %prec NEG { $$ = new ParserVal(-$2.dval); } exp '^' exp { $$=new ParserVal(Math.pow($1.dval, $3.dval));} '(' exp ')' { $$ = $2; } ; %%...

%% String ins; StringTokenizer st; void yyerror(string s){ System.out.println("par:"+s); } boolean newline; int yylex(){ String s; int tok; Double d; if (!st.hasmoretokens()) if (!newline) { newline=true; return \n'; //So we look like classic YACC example } else return 0; s = st.nexttoken(); try { d = Double.valueOf(s); /*this may fail*/ yylval = new ParserVal(d.doubleValue()); //SEE BELOW tok = NUM; } catch (Exception e) { tok = s.charat(0);/*if not float, return char*/ } return tok; }

void dotest(){ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("BYACC/J Calculator Demo"); System.out.println("Note: Since this example uses the StringTokenizer"); System.out.println("for simplicity, you will need to separate the items"); System.out.println("with spaces, i.e.: '( 3 + 5 ) * 2'"); while (true) { System.out.print("expression:"); try { ins = in.readline(); } catch (Exception e) { } st = new StringTokenizer(ins); newline=false; yyparse(); } } public static void main(string args[]){ Parser par = new Parser(false); par.dotest(); }

Parser states Not exactly elements of PDA s Q, but similar A yacc "state" is a set of "dotted rules" a grammar rules with a "dot somewhere in the right hand side. (In some yacc printouts, "_" is the dot.) Intuitively, "A α_β" in a state means this rule, up to and including α is consistent with input seen so far; next terminal in the input might derive from the left end of β. E.g., before reading any input, "S _ β" is consistent, for every rule S β " (S = start symbol) Yacc deduces legal shift/goto actions from terminals/ nonterminals following dot; reduce actions from rules with dot at rightmost end. See examples below

Yacc Output: Random Example 0 $accept : S $end 1 S : 'a' 'b' C 'd' 2 'a' 'e' F 'g' 3 C : 'h' C 4 'h' 5 F : 'h' F 6 'h' state 0 $accept :. S $end (0) 'a' shift 1 S goto 2 state 1 S : 'a'. 'b' C 'd' (1) S : 'a'. 'e' F 'g' (2) 'b' shift 3 'e' shift 4 state 2 $accept : S. $end (0) $end accept state 3 S : 'a' 'b'. C 'd' (1) 'h' shift 5 C goto 6 state 4 S : 'a' 'e'. F 'g' (2) 'h' shift 7 F goto 8 state 5 C : 'h'. C (3) C : 'h'. (4) 'h' shift 5 'd' reduce 4 C goto 9 state 6 S : 'a' 'b' C. 'd' (1) 'd' shift 10 state 7 F : 'h'. F (5) F : 'h'. (6) 'h' shift 7 'g' reduce 6 F goto 11 state 8 S : 'a' 'e' F. 'g' (2) 'g' shift 12 state 9 C : 'h' C. (3). reduce 3 state 10 S : 'a' 'b' C 'd'. (1). reduce 1 state 11 F : 'h' F. (5). reduce 5 state 12 S : 'a' 'e' F 'g'. (2). reduce 2

State Diagram (partial) 0 $accept : S $end 1 S : 'a' 'b' C 'd' 2 'a' 'e' F 'g' 3 C : 'h' F 4 'h' 5 F : 'h' F 6 'h' h state 0 $acc :. S $end S :. 'a' 'b' C 'd' S :. 'a' 'e' F 'g' 'a' shift 1 S goto 2 state 1 S : 'a'. 'b' C 'd (1) S : 'a'. 'e' F 'g (2) 'b' shift 3 'e' shift 4 b a e S state 2 $acc : S. $end $end accept $end accept state 5 C : 'h'. C C : 'h'. 'h' shift 5 'd' reduce 4 C goto 9 h state 3 S : 'a' 'b'. C 'd' (1) 'h' shift 5 C goto 6 state 4 S : 'a' 'e'. F 'g' (2) 'h' shift 7 F goto 8 C C state 9 C : 'h' C. state 6 S : 'a' 'b' C. 'd' (1) d state 10 S : 'a' 'b' C 'd'. (1). reduce 3 'd' shift 10. reduce 1

Yacc "Parser Table" expr: expr '+' term term ; term: term '*' fact fact ; fact: '(' expr ')' 'A' ; State Dotted Rules Shift Actions Goto Actions A + * ( ) $end expr term fact (default) 0 $accept : _expr $end 5 4 1 2 3 error 1 $accept : expr_$end expr : expr_+ term 6 accept error 2 expr : term_ (2) term : term_* fact 7 reduce 2 3 term : fact_ (4) reduce 4 4 fact : (_expr ) 5 4 8 2 3 error 5 fact : A_ (6) reduce 6 6 expr : expr +_term 5 4 9 3 error 7 term : term *_fact 5 4 10 error 8 expr : expr_+ term fact : ( expr_) 6 11 error 9 expr : expr + term_ (1) term : term_* fact 7 reduce 1 10 term : term * fact_ (3) reduce 3 11 fact : ( expr )_ (5) reduce 5

Yacc Output shift/goto # # is a state # reduce # # is a rule # A : β _ (#) # is this rule #. default action state 0 $accept : _expr $end ( shift 4 A shift 5 expr goto 1 term goto 2 fact goto 3 state 1 $accept : expr_$end expr : expr_+ term $end accept + shift 6 state 2 expr : term_ (2) term : term_* fact... * shift 7. reduce 2

Implicit Dotted Rules state 0 $accept : _expr $end $accept: _ expr $end expr: _ expr '+ term expr: _ term ( shift 4 A shift 5 expr goto 1 term goto 2 fact goto 3 term: term: fact: fact: _ term '*' fact _ fact _ '(' expr ')' _ 'A'

Goto & Lookahead state 0 $accept : _expr $end $accept: _ expr $end expr: _ expr '+ term expr: _ term ( shift 4 A shift 5 expr goto 1 term goto 2 fact goto 3 term: term: fact: fact: _ term '*' fact _ fact _ '(' expr ')' _ 'A'

using the unambiguous expression grammar Example: input "A + A $end" Action: Stack: Input: shift 5 reduce fact A, go 3 state 5 says reduce rule 6 on +; state 0 (exposed on pop) says goto 3 on fact reduce fact term, go 2 reduce expr term, go 1 shift 6 0 A + A $end 0 A 5 + A $end 0 fact 3 + A $end 0 term 2 + A $end 0 expr 1 + A $end

Action: Stack: Input: shift 6 shift 5 reduce fact A, go 3 reduce term fact, go 9 reduce expr expr + term, go 1 accept 0 expr 1 + 6 A $end 0 expr 1 + 6 A 5 $end 0 expr 1 + 6 fact 3 $end 0 expr 1 + 6 term 9 $end 0 expr 1 $end

An Error Case: "A ) $end": Action: Stack: Input: 0 A ) $end shift 5 0 A 5 ) $end reduce fact A, go 3 0 fact 3 ) $end reduce fact term, go 2 0 term 2 ) $end reduce expr term, go 1 0 expr 1 ) $end error