Yacc Yet Another Compiler Compiler

Similar documents
Syntax Analysis Part IV

Using an LALR(1) Parser Generator

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

CSCI Compiler Design

Principles of Programming Languages

Yacc: A Syntactic Analysers Generator

Syntax-Directed Translation

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

Introduction to Lex & Yacc. (flex & bison)

An Introduction to LEX and YACC. SYSC Programming Languages

LECTURE 11. Semantic Analysis and Yacc

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

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

PRACTICAL CLASS: Flex & Bison

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

TDDD55 - Compilers and Interpreters Lesson 3

Context-free grammars

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

Compiler Construction: Parsing

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

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

TDDD55- Compilers and Interpreters Lesson 3

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

Lexical and Syntax Analysis

CS143 Handout 12 Summer 2011 July 1 st, 2011 Introduction to bison

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

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

Automatic Scanning and Parsing using LEX and YACC

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

Parsing How parser works?

Compiler Lab. Introduction to tools Lex and Yacc

COMPILER CONSTRUCTION Seminar 02 TDDB44

Hyacc comes under the GNU General Public License (Except the hyaccpar file, which comes under BSD License)

Syn S t yn a t x a Ana x lysi y s si 1

CSE302: Compiler Design

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

Applications of Context-Free Grammars (CFG)

Compiler Construction

Parsing. COMP 520: Compiler Design (4 credits) Professor Laurie Hendren.

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

Preparing for the ACW Languages & Compilers

Compiler Construction

Gechstudentszone.wordpress.com

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

Compiler construction in4020 lecture 5

COP 3402 Systems Software Syntax Analysis (Parser)

Chapter 2: Syntax Directed Translation and YACC

(F)lex & Bison/Yacc. Language Tools for C/C++ CS 550 Programming Languages. Alexander Gutierrez

Conflicts in LR Parsing and More LR Parsing Types

Lesson 10. CDT301 Compiler Theory, Spring 2011 Teacher: Linus Källberg

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Lexical and Parser Tools

Introduction to Compiler Design

CS 403: Scanning and Parsing

Yacc. Generator of LALR(1) parsers. YACC = Yet Another Compiler Compiler symptom of two facts: Compiler. Compiler. Parser

Compiler construction 2002 week 5

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

LECTURE 7. Lex and Intro to Parsing

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process.

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

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

Compiler construction 2005 lecture 5

Topic 5: Syntax Analysis III

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

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Building a Parser Part III

Lecture 8: Deterministic Bottom-Up Parsing

UNIT III & IV. Bottom up parsing

Syntax Analysis The Parser Generator (BYacc/J)

Compiler Construction Assignment 3 Spring 2018

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

Lecture 7: Deterministic Bottom-Up Parsing

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

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

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Lexical and Syntax Analysis (2)

shift-reduce parsing

Bottom-Up Parsing. Lecture 11-12

A Simple Syntax-Directed Translator

L L G E N. Generator of syntax analyzier (parser)

Abstract Syntax. Mooly Sagiv. html://

POLITECNICO DI TORINO. (01JEUHT) Formal Languages and Compilers. Laboratory N 3. Lab 3. Cup Advanced Use

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Bottom-Up Parsing. Lecture 11-12

Compiler Front-End. Compiler Back-End. Specific Examples

Compiler Construction

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

CS 11 Ocaml track: lecture 6

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

SOFTWARE ARCHITECTURE 5. COMPILER

CS415 Compilers. LR Parsing & Error Recovery

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

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(..!

Programming Language Syntax and Analysis

Lex and Yacc. More Details

Transcription:

LEX and YACC work as a team Yacc Yet Another Compiler Compiler How to work? Some material adapted from slides by Andy D. Pimentel LEX and YACC work as a team Availability call yylex() NUM + NUM next token is NUM [0-9]+ lex, yacc on most UNIX systems bison: a yacc replacement from GNU flex: fast lexical analyzer BSD yacc Windows/MS- DOS versions exist 1

YACC s Basic Opera>onal Sequence YACC File Format File containing desired grammar in YACC format Definitions yacc y.tab.c C source program created by YACC %% Rules The identical LEX format was taken from this... cc or gcc %% a.out Executable program that will parse grammar given in gram.y Supplementary Code Rules Sec>on Defini>ons sec>on example A context free grammar, e.g.: expr term : expr '+' term term : term '*' factor factor factor : '(' expr ')' ID NUM %{ #include <stdio.h> #include <stdlib.h> %} %token ID NUM %start expr This is called a terminal The start symbol (non- terminal) 2

Some details LEX produces a funcxon called yylex() YACC produces a funcxon called yyparse() yyparse() expects to be able to call yylex() How to get yylex()? Write your own! If you don't want to write your own: use lex! If you wanted to write your own int yylex() { if(it's a num) return NUM else if(it's an id) return ID else if(parsing is done) return 0 else if(it's an error) return -1 } Seman>c ac>ons Seman>c ac>ons expr : expr '+' term { $$ = $1 + $3 } term { $$ = $1 } term : term '*' factor { $$ = $1 * $3 } factor { $$ = $1 } factor : '(' expr ')' { $$ = $2 } ID NUM expr : expr '+' term { $$ = $1 + $3 } term { $$ = $1 } term : term '*' factor { $$ = $1 * $3 } factor { $$ = $1 } factor : '(' expr ')' { $$ = $2 } ID NUM 3

Seman>c ac>ons (cont d) Seman>c ac>ons (cont d) Seman>c ac>ons (cont d) Precedence / Associa>on (1) 1 2-3 (2) 1 2 * 3 1. 1-2- 3 = (1-2)- 3? or 1- (2-3)? Define - operator is leb- associaxon. 2. 1-2*3 = 1- (2*3) Define * operator is precedent to - operator 4

Precedence / Associa>on %left '+' '-' %left '*' '/' %noassoc UMINUS expr : expr + expr { $$ = $1 + $3 } expr - expr { $$ = $1 - $3 } expr * expr { $$ = $1 * $3 } expr / expr { if($3==0) yyerror( divide 0 ) else $$ = $1 / $3 } - expr %prec UMINUS {$$ = -$2 } Precedence / Associa>on %right = %left '<' '>' NE LE GE %left '+' '- %left '*' '/' highest precedence! GeSng YACC & LEX to work together cc/ gcc Building Example Suppose you have a lex file called scanner.l and a yacc file called decl.y and want parser Steps to build... lex scanner.l yacc -d decl.y gcc -c lex.yy.c y.tab.c gcc -o parser lex.yy.o y.tab.o -ll Note: scanner should include in the definixons secxon: #include "y.tab.h" 5

YACC Rules may be recursive Rules may be ambiguous Uses boiom- up Shib/Reduce parsing Get a token Push onto stack Can it be reduced (How do we know?) If yes: Reduce using a rule If no: Get another token YACC can t look ahead > 1 token = exp a = 7 b = 3 + a + 2 = exp NAME = exp NAME = = 7 b = 3 + a + 2 7 b = 3 + a + 2 6

= exp NAME = 7 = exp NAME = exp b = 3 + a + 2 b = 3 + a + 2 = exp stmt = exp stmt b = 3 + a + 2 b = 3 + a + 2 7

= exp stmt NAME = 3 + a + 2 = exp stmt NAME = 3 + a + 2 = exp stmt NAME = NUMBER = exp stmt NAME = exp + a + 2 + a + 2 8

= exp stmt NAME = exp + = exp stmt NAME = exp + NAME a + 2 + 2 = exp = exp stmt NAME = exp + exp stmt NAME = exp + 2 + 2 9

= exp stmt NAME = exp + = exp stmt NAME = exp + NUMBER 2 = exp stmt NAME = exp + exp = exp stmt NAME = exp 10

= exp stmt stmt = exp stmt IF- ELSE Ambiguity = exp DONE! stmt Consider following rule: Following state : Two possible derivaxons: IF expr IF expr stmt. ELSE stmt IF expr IF expr stmt ELSE. stmt IF expr IF expr stmt ELSE stmt. IF expr stmt IF expr IF expr stmt. ELSE stmt IF expr IF expr stmt. ELSE stmt IF expr stmt. ELSE stmt IF expr stmt ELSE. stmt IF expr stmt ELSE stmt. 11

IF- ELSE Ambiguity IF- ELSE Ambiguity It is a shib/reduce conflict YACC will always do shib first SoluXon 1 : re- write grammar SoluXon 2: the rule has the same precedence as token IFX ShiV/Reduce Conflicts shiv/reduce conflict occurs when a grammar is wriien in such a way that a decision between shibing and reducing can not be made. e.g.: IF- ELSE ambiguity To resolve conflict, YACC will choose to shib Reduce/Reduce Conflicts Reduce/Reduce Conflicts: start : expr stmt expr : CONSTANT stmt : CONSTANT YACC (Bison) resolves conflict by reducing using rule that is earlier in grammar Not good pracxce to rely on this So, modify grammar to eliminate them 12

Error Messages Bad error message: Syntax error Compiler needs to give programmer a good advice It is beier to track the line number in LEX: Leb recursion Right recursion Use lev recursion in yacc LR parser prefers leb recursion LL parser prefers right recursion Yacc is a LR parser: use leb recursion `%start' YACC Declara>on Summary Specify grammar's start symbol `%union Declare collecxon of data types that semanxc values may have `%token Declare terminal symbol (token type name) with no precedence or associaxvity specified `%type Declare the type of semanxc values for a nonterminal symbol YACC Declara>on Summary `%right Declare terminal symbol (token type name) that is right- associaxve `%lev Declare terminal symbol (token type name) that is leb- associaxve `%nonassoc Declare terminal symbol (token type name) that is nonassociaxve using it as associaxve is a syntax error, e.g.: x op. y op. z is syntax error 13