LECTURE 11. Semantic Analysis and Yacc

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

Syntax Analysis Part IV

PRACTICAL CLASS: Flex & Bison

An Introduction to LEX and YACC. SYSC Programming Languages

Compiler Lab. Introduction to tools Lex and Yacc

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:

Yacc: A Syntactic Analysers Generator

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

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

Introduction to Lex & Yacc. (flex & bison)

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

Lexical and Parser Tools

Using an LALR(1) Parser Generator

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

Automatic Scanning and Parsing using LEX and YACC

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

CSE302: Compiler Design

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

CSCI Compiler Design

Yacc Yet Another Compiler Compiler

Syntax Analysis The Parser Generator (BYacc/J)

Syntax-Directed Translation

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

LECTURE 7. Lex and Intro to Parsing

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

Parsing How parser works?

Lexical and Syntax Analysis

Gechstudentszone.wordpress.com

TDDD55 - Compilers and Interpreters Lesson 3

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

UNIT III & IV. Bottom up parsing

TDDD55- Compilers and Interpreters Lesson 3

Principles of Programming Languages

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

Syntax Analysis Part VIII

COP4020 Programming Assignment 1 CALC Interpreter/Translator Due March 4, 2015

CSC 467 Lecture 3: Regular Expressions

Compil M1 : Front-End

COMPILER CONSTRUCTION Seminar 02 TDDB44

Chapter 3 -- Scanner (Lexical Analyzer)

Parsing and Pattern Recognition

TDDD55- Compilers and Interpreters Lesson 2

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

Applications of Context-Free Grammars (CFG)

Chapter 2: Syntax Directed Translation and YACC

Compiler construction in4020 lecture 5

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

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

Lexical Analysis. Implementing Scanners & LEX: A Lexical Analyzer Tool

Figure 2.1: Role of Lexical Analyzer

Compiler construction 2002 week 5

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.

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

Preparing for the ACW Languages & Compilers

Context-free grammars

LEX/Flex Scanner Generator

Gechstudentszone.wordpress.com

Compiler Construction: Parsing

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

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

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

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

Compiler construction 2005 lecture 5

Lexical Analysis. Introduction

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

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

Group A Assignment 3(2)

Handout 7, Lex (5/30/2001)

Over-simplified history of programming languages

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

UNIT - 7 LEX AND YACC - 1

IBM. UNIX System Services Programming Tools. z/os. Version 2 Release 3 SA

Using Lex or Flex. Prof. James L. Frankel Harvard University

Compiler Construction Assignment 3 Spring 2018

CS 403: Scanning and Parsing

Lex (Lesk & Schmidt[Lesk75]) was developed in the early to mid- 70 s.

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

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

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

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

CMSC445 Compiler design Blaheta. Project 2: Lexer. Due: 15 February 2012

PRINCIPLES OF COMPILER DESIGN UNIT II LEXICAL ANALYSIS 2.1 Lexical Analysis - The Role of the Lexical Analyzer

LECTURE 6 Scanning Part 2

Chapter 3 Lexical Analysis

A simple syntax-directed

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

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

Scanning. COMP 520: Compiler Design (4 credits) Alexander Krolik MWF 13:30-14:30, MD 279

Topic 5: Syntax Analysis III

Compiler course. Chapter 3 Lexical Analysis

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

Compiler Construction

I. OVERVIEW 1 II. INTRODUCTION 3 III. OPERATING PROCEDURE 5 IV. PCLEX 10 V. PCYACC 21. Table of Contents

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

An introduction to Flex

Ulex: A Lexical Analyzer Generator for Unicon

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Transcription:

LECTURE 11 Semantic Analysis and Yacc

REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely specifying valid structures with a context-free grammar, we can attach meaning to the structures with an attribute grammar. Attribute grammars contain productions, as in a context-free grammar, as well as semantic actions to perform when the production is taken. production semantic rule <A> <B> <C> A.a :=...; B.a :=...; C.a :=...

REVIEW OF LAST LECTURE We decorate a parse tree by evaluating the attributes of the nodes. The attributes may have a complicated dependency hierarchy. An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used). Some attribute grammars, such as S-attributed and L-attributed grammars, give rise to straightforward attribute flow algorithms (such as one depth-first traversal of the tree).

ATTRIBUTE GRAMMAR EXAMPLES P A B C A 1 A 2 a A a B 1 B 2 b B b C 1 C 2 c C c Write the semantic actions such that P has an attribute st whose value is true when the number of a s, b s and c s are the same in the sentence, and false otherwise.

ATTRIBUTE GRAMMAR EXAMPLES P A B C A 1 A 2 a A a {A. num = 1; } B 1 B 2 b B b {B. num = 1; } C 1 C 2 c C c {C. num = 1; } Write the semantic actions such that P has an attribute st whose value is true when the number of a s, b s and c s are the same in the sentence, and false otherwise.

ATTRIBUTE GRAMMAR EXAMPLES P A B C A 1 A 2 a {A 1. num = A 2. num + 1; } A a {A. num = 1; } B 1 B 2 b {B 1. num = B 2. num + 1; } B b {B. num = 1; } C 1 C 2 c {C 1. num = C 2. num + 1; } C c {C. num = 1; } Write the semantic actions such that P has an attribute st whose value is true when the number of a s, b s and c s are the same in the sentence, and false otherwise.

ATTRIBUTE GRAMMAR EXAMPLES P A B C { if ((A. num == B. num) && (A. num == C. num)) P. st = true; else P. st = false; } A 1 A 2 a {A 1. num = A 2. num + 1; } A a {A. num = 1; } B 1 B 2 b {B 1. num = B 2. num + 1; } B b {B. num = 1; } C 1 C 2 c {C 1. num = C 2. num + 1; } C c {C. num = 1; } Write the semantic actions such that P has an attribute st whose value is true when the number of a s, b s and c s are the same in the sentence, and false otherwise.

ATTRIBUTE GRAMMAR EXAMPLES Here s another example grammar. P W W B W 1 B W 2 B 1 B 0 Let s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES P W W B W 1 B W 2 B 1 {B. num = 1; } B 0 {B. num = 0; } Let s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES P W W B {W. val = B. num; W. order = 1; } W 1 B W 2 B 1 {B. num = 1; } B 0 {B. num = 0; } Let s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES P W W B {W. val = B. num; W. order = 1; } W 1 B W 2 {W 1. order = W 2. order 2; W 1. val = W 2. val + (B. num W 1. order); } B 1 {B. num = 1; } B 0 {B. num = 0; } Let s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES P W {printf("%d", W. val); } W B {W. val = B. num; W. order = 1; } W 1 B W 2 {W 1. order = W 2. order 2; W 1. val = W 2. val + (B. num W 1. order); } B 1 {B. num = 1; } B 0 {B. num = 0; } Let s write an attribute grammar that converts the binary number to a decimal number and prints it out.

INTRODUCTION TO YACC Yacc (Yet Another Compiler Compiler) Automatically generate an LALR (Look-ahead LR) parser for a context free grammar. Parses a subset of LR grammars. Can parse LL(1) grammars as long as symbols with empty derivations also have nonempty derivations. Allows syntax-directed translation by writing grammar productions and semantic actions. Works with Lex by calling yylex() to get the next token.

YACC Yacc file format: declarations /* specify tokens, non-terminals, and other C constructs */ %% translation rules /* specify grammar here */ %% supporting C-routines The command yacc yaccfile produces y.tab.c, which contains a routine yyparse(). yyparse() calls yylex() to get tokens. yyparse() returns 0 if the program is grammatically correct, non-zero otherwise.

YACC DECLARATIONS %{ %} #include <stdio.h> #include <string.h> %token NUMBER NAME Include some necessary libraries. Specify the tokens we will be using. %% %%

YACC TRANSLATION RULES statement: expression: NAME '=' expression expression ; expression '+' NUMBER expression '-' NUMBER NUMBER ; S name = E S E E E + num E E num E num

YACC ATTRIBUTE GRAMMARS Each symbol can be associated with some attributes and semantic actions can be associated with productions. item : LPAREN exp RPAREN {$$ = $2;} NUMBER {$$ = $1;} ; $$ is the attribute associated with the left-hand side of the production. $1 is the attribute associated with the first symbol in the right-hand side, $2 for the second symbol,

YACC ENVIRONMENT Yacc processes a yacc specification file and produces a y.tab.c file. An integer function yyparse() is produced by Yacc. Calls yylex() to get tokens. Return non-zero when an error is found, or 0 if the program is accepted. Need main() and and yyerror() functions. yyerror(const char *str){ printf("yyerror: %s at line %d\n", str, yyline); } int main(){ return yyparse(); }

LEX AND YACC TOGETHER ex.l %{ #include y.tab.h" extern int yylval; %} Include the header file for the parser. yylval accessible by Yacc as image of token. %% [0-9]+ { yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} %% What kinds of tokens does this lex file identify?

LEX AND YACC TOGETHER ex.l %{ #include y.tab.h" extern int yylval; %} Include the header file for the parser. yylval accessible by Yacc as image of token (note that it is of type int here). %% [0-9]+ { yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} %% What kinds of tokens does this lex file identify? - Integers - Whitespace - Newlines - Single characters that do not fall into any other category

LEX AND YACC TOGETHER %{ #include <stdio.h> %} %token NAME NUMBER %% statement: NAME '=' expression { printf("pretending to assign %s the value %d\n", $1, $3); } expression { printf("= %d\n", $1); } ; expression: expression '+' NUMBER { $$ = $1 + $3; printf ("Recognized '+' expression.\n");} expression '-' NUMBER { $$ = $1 - $3; printf ("Recognized '-' expression.\n");} NUMBER { $$ = $1; printf ("Recognized a number.\n");} ; %% int main (void) { return yyparse();} int yyerror (char *msg) { return fprintf (stderr, "YACC: %s\n", msg); } int yywrap(){ return 1; }

LEX AND YACC TOGETHER carnahan@diablo:~>flex ex.l carnahan@diablo:~>yacc -d ex.y carnahan@diablo:~>gcc lex.yy.c y.tab.c carnahan@diablo:~>./a.out < test.txt scanned the number 3 Recognized a number. skipped whitespace found other data "+" skipped whitespace scanned the number 4 Recognized '+' expression. reached end of line = 7 test.txt 3 + 4 yacc d: cause the header file y.tab.h to be written.

LEX AND YACC TOGETHER ex.l %{ #include y.tab.h" extern int yylval; %} Notice that yylval is an int here. What about when we want the image of strings? %% [0-9]+ { yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} %%

LEX AND YACC TOGETHER Let s make yylval a union so that it can represent multiple types. In our yacc file: %{ #include <stdio.h> %} %union { int number; char *string; } %token <string> NAME %token <number> NUMBER %type <number> expression %% Makes yylval a union of int and char* Now, we specify the type we need to use from the union for not only the tokens but also the non-terminals which are used in semantic actions.

LEX AND YACC TOGETHER In our lex file: %{ #include "y.tab.h" %} Now, we must specify the type as an attribute of yylval. Also, we added rules for picking up the NAME token. %% [a-za-z]+ {yylval.string=yytext; printf( scanned the name %s\n, yylval); return NAME;} [0-9]+ { yylval.number = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); }.

LEX AND YACC TOGETHER carnahan@diablo:~>lex ex.l carnahan@diablo:~>yacc -d ex.y carnahan@diablo:~>gcc lex.yy.c y.tab.c carnahan@diablo:~>./a.out < test.txt scanned the number a skipped whitespace found other data "=" skipped whitespace scanned the number 3 Recognized a number. skipped whitespace found other data "+" skipped whitespace scanned the number 4 Recognized '+' expression. reached end of line pretending to assign a = 3 + 4 the value 7

MULTIPLE ATTRIBUTES It is possible to define multiple attributes for a symbol by adding structs to your union. For example, let s say you want a symbol to be associated with both a type and id. %union{ struct{ int type; char *id; } type_id; } %type <type_id> var

CONFLICTS Keep in mind that Yacc produces an LALR parser. If the grammar is not LALR, there may be conflicts that prevent parsing. Yacc will try to resolve them automatically, but some conflicts will need your intervention. Where it is necessary, specify precedence and associativity of operators: use keywords %left, %right, %nonassoc in the declarations section. - All tokens on the same line are the same precedence level and associativity. - The lines are listed in order of increasing precedence. %left PLUSnumber, MINUSnumber %left TIMESnumber, DIVIDEnumber