Compiler Lab. Introduction to tools Lex and Yacc

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

Yacc: A Syntactic Analysers Generator

Using an LALR(1) Parser Generator

Syntax Analysis Part IV

CSCI Compiler Design

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

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

CSE302: Compiler Design

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:

An Introduction to LEX and YACC. SYSC Programming Languages

PRACTICAL CLASS: Flex & Bison

Preparing for the ACW Languages & Compilers

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.

Parsing How parser works?

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

Introduction to Lex & Yacc. (flex & bison)

Syntax-Directed Translation

Syntax Analysis The Parser Generator (BYacc/J)

Compil M1 : Front-End

Chapter 2: Syntax Directed Translation and YACC

Principles of Programming Languages

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

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

Lexical and Syntax Analysis

Yacc Yet Another Compiler Compiler

Applications of Context-Free Grammars (CFG)

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

COMPILER CONSTRUCTION Seminar 02 TDDB44

TDDD55- Compilers and Interpreters Lesson 3

UNIT III & IV. Bottom up parsing

Compiler Construction Assignment 3 Spring 2018

TDDD55 - Compilers and Interpreters Lesson 3

Context-free grammars

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

Lecture 14 Sections Mon, Mar 2, 2009

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Compiler construction in4020 lecture 5

Compiler Construction: Parsing

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

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

UNIVERSITY OF CALIFORNIA

Compiler construction 2002 week 5

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

Lexical and Parser Tools

COP5621 Exam 3 - Spring 2005

Introduction to Compiler Design

Figure 2.1: Role of Lexical Analyzer

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

Syntax-Directed Translation. Introduction

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

Programming Language Syntax and Analysis

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Compiler Construction

Compiler Construction

LECTURE 7. Lex and Intro to Parsing

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

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

Automatic Scanning and Parsing using LEX and YACC

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

Compiler construction 2005 lecture 5

Chapter 3 -- Scanner (Lexical Analyzer)

CS4850 SummerII Lex Primer. Usage Paradigm of Lex. Lex is a tool for creating lexical analyzers. Lexical analyzers tokenize input streams.

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

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Parser Tools: lex and yacc-style Parsing

TDDD55- Compilers and Interpreters Lesson 2

Parser Tools: lex and yacc-style Parsing

Module 8 - Lexical Analyzer Generator. 8.1 Need for a Tool. 8.2 Lexical Analyzer Generator Tool

Gechstudentszone.wordpress.com

Syntax-Directed Translation

Syntax-Directed Translation

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

Chapter 3 Lexical Analysis

Front End. Hwansoo Han

How do LL(1) Parsers Build Syntax Trees?

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

Compiler course. Chapter 3 Lexical Analysis

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

Lex and Yacc. A Quick Tour

IN4305 Engineering project Compiler construction

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

Parsers. Chapitre Language

Undergraduate Compilers in a Day

Lab 2. Lexing and Parsing with Flex and Bison - 2 labs

COP 3402 Systems Software Syntax Analysis (Parser)

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

Syntax Analysis Part VIII

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

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

A simple syntax-directed

Chapter 4. Lexical and Syntax Analysis

Component Compilers. Abstract

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

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

Compiler Construction

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Etienne Bernard eb/textes/minimanlexyacc-english.html

CS 403: Scanning and Parsing

Transcription:

Compiler Lab Introduction to tools Lex and Yacc

Assignment1 Implement a simple calculator with tokens recognized using Lex/Flex and parsing and semantic actions done using Yacc/Bison.

Calculator Input: 12 + 34 * 6 Meaningful lexical units 12 + 34 * 6 Syntactically valid valuate? 12+34* is not syntactically valid

Input: 12 + 34 * 6 Lexical units 12 + 34 * 6 Separate into tokens : Calculator <NUM, 12> <ARITHOP, +> <NUM, 34> <ARITHOP, *> <NUM, 6> Use a Lexical Analyzer Patterns for various tokens

Input: 12 + 34 * 6 Calculator Lexical Analyzer gives tokens : <NUM, 12> <ARITHOP, +> <NUM, 34> <ARITHOP, *> <NUM, 6> Check the syntax (12+34* is not syntactically valid) Use Syntax Analyzer (parser) Grammar rules valuate?

xpression Lexical Token Parser Analyzer Getnext Token

Calculator : Lexical Analysis

Flex / Lex A Lexical analyzer generator Input : Patterns (similar to regular expressions) Generates C code for a lexical analyzer(scanner) Lex program Lex Compiler C Code for Lexical Analyzer lex.yy.c C Compiler Lexical Analyzer (executable)

A portion of Lex program %% [0 9] { return NUM; } [A Z] { return ID; } %% Pattern Action

//Declaration section digit [0 9] // regular definition letter [a za Z] %% //Rules section {digit}+ {letter} ({letter} {digit})* { return NUM;} { return ID;} %%

%% {digit}+ {yylval = atoi (yytext); return NUM;} %% yytext : pointer to the string matched (lexeme) yylval : attribute value for the token

Lexical Analyzer, Parser communication LA should pass a token name and associated attribute value to the parser e. g. <NUM, 123> return NUM returns a token name to the parser yylval = atoi (yytext) attribute value associated with the token

//lex file, calcu.l %{ #include "y.tab.h" extern int yylval; %} digit [0 9] %% {digit}+ {yylval = atoi (yytext); return NUM;} [*+\n] { return *yytext;}. { } %%

Calculator: Parsing

Bison / Yacc A parser generator Input : Context Free Grammar rules for the language Generates C code for a parser for your language

An example > + > 0 1 2 Some strings in the language 1+2 1+0+2 2+2+1+0 Some strings not in the language 1+ +2+3 1++ 12

Parse Tree Input : 1+2 + 1 2

Parse Tree + 1 2 Parse Tree for 1+2+1 1+ +2+3

> + > 0 1 2 Syntactic validity Valid strings: 1+2, 1+0+2, 2+2+1+0... Can build a parse tree for any of these strings Invalid strings:1+, +2+3, 1++, 12 Parse tree construction fails

Parsing Algorithm Input: Grammar, string to be checked Output: Yes / No (string is syntactically valid or not) Parser simulates the steps in building a parse tree for the given string Two approaches Top down, Bottom up

Input : 1+2 Top down Parsing + + + 1 1 2 Builds a parse tree top down, starting from the root

Bottom up Parsing Input : 1+2 1 + 2 1 + 2 1 + 2 Builds a parse tree bottom up, starting from the leaves A sequence of reductions

xample Grammar > + > NUM NUM is a token returned by the Lexical Analyzer, for an input matching [0 9]+

Input : 1+2 Tokens: NUM, +, NUM Parse Tree + NUM (1) NUM (2)

Bottom up parsing Implementation using a stack Input: 1 + 2 Tokens: NUM + NUM Stack Input Action NUM + NUM Stack mpty, shift NUM NUM NUM + NUM NUM on stack, reduce using the production > NUM NUM + NUM shift +

Bottom up parsing Implementation using a stack Input: 1 + 2 Tokens: NUM + NUM Stack Input Action + NUM + NUM shift NUM NUM + NUM + NUM NUM on stack, reduce using the production > NUM + NUM + NUM + on stack, reduce using the production > +

Bottom up parsing Implementation using a stack Input: 1 + 2 Tokens: NUM + NUM Stack Input Action NUM + NUM on stack, no more input tokens, accept the string Action of the parser on input 1+2+?

Shift / reduce parser A sequence of Shift / Reduce actions and finally accept/reject YACC generates Shift/reduce parser LALR parser, a kind of

Given grammar rules: S > > + > NUM Yacc Specification %% pgm : expr {... } ; expr : expr '+' expr {...} ; expr : NUM {...} ; %%

Calculator: valuation

valuation during parsing Input : 1 + 2 val = 3 val = 1 + val =2 NUM val =1 val =2 NUM

valuation during parsing Input : 1 + 2.val = 3.val = 1 +.val =2 NUM NUM.val =1 NUM NUM.val =2

Syntax Directed Definition Associate an attribute with each grammar symbol NUM associated numeric value, lexval, denoted NUM.lexval an attribute, val, denoted.val

> 1 + 2 {.val = 1.val + 2.val } > NUM {.val = NUM. lexval } Semantic actions with each production computes the attribute value Action taken when the parser reduces using the corresponding production

Input : 1 + 2 Annotated parse tree.val = 3.val = 1 +.val =2 NUM.lexval =1 NUM.lexval =2

valuation during parsing: Implementation Parser maintains a value stack Lexical Analyzer sets yylval the value put on top of the value stack Type of value stack default type integer

Bottom up parsing Attribute evaluation Input: 1 + 2 Tokens: NUM + NUM Parser Stack Value Stack NUM + NUM Action Stack mpty, shift NUM NUM 1 NUM + NUM NUM on stack, 1 on value stack reduce using the production > NUM 1 NUM + NUM shift +

Bottom up parsing Attribute evaluation Input: 1 + 2 Tokens: NUM + NUM Stack Input Action + 1 NUM + NUM shift NUM NUM + 2 1 NUM + NUM NUM on stack, 2 on value stack reduce (production >NUM)

Bottom up parsing Attribute evaluation Input: 1 + 2 Tokens: NUM + NUM Stack Input Action + 2 + on stack, NUM + NUM reduce (production > + ) compute the attribute value.val = 1.val + 2.val 1 3 NUM + NUM on stack, value 3 on value stack

> 1 + 2 {.val = 1.val + 2.val } > NUM {.val = NUM. lexval } In YACC > 1 + 2 { $$ = $1 + $3 } > NUM { $$ = $1 } $i : attribute value of grammar symbol i on the right hand side. $$ : attribute value of grammar symbol on the left hand side. By default, each attribute is of type integer

//Yacc file, calcu.y %{ #include "lex.yy.c" %} %token NUM %% pgm : expr '\n' {printf("%d\n", $1); exit(0);} ; expr : expr '+' expr {$$= $1+$3;} ; expr : NUM {$$=$1;} ; %% int main(void){ return yyparse();}

Building the calculator lex calcu.l yacc d calcu.y cc y.tab.c ll./a.out

Building an expression tree

Semantic actions to build an expression tree > 1 + 2 {.ptr = mknode (+, 1.ptr, 2.ptr) } > NUM {.ptr = mkleafnode (NUM.lexval) } YACC code expr : expr '+' expr {$$=mknode('+', $1, $3);} ; expr : NUM {$$=mkleafnode($1);}

xpression Tree %union{ //defines YYSTYP int ival; struct tree_node *nptr; }; %token <ival> NUM %type <nptr> expr

%{... #include "exprtree.h"... %} %union{ int ival; struct tree_node *nptr; }; %token <ival> NUM %type <nptr> expr %% pgm: expr '\n' {printf("%d\n", evaluate($1));} ; expr: expr '+' expr {$$=mkoperatornode('+', $1, $3);} ; expr: NUM {$$=mkleafnode($1);} ; %%