2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Parser construction tools: YACC
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LEX and YACC work as a team
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Introduction Fan Wu Department of Computer Science and Engineering
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Lex.
By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.
Syntactic Analysis Tools
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
LESSON 04.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Introduction to YACC CS 540 George Mason University.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
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.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Chapter 2: A Simple One Pass Compiler
Chapter 3 – Describing Syntax
Syntax Analysis Part III
A Simple Syntax-Directed Translator
Textbook:Modern Compiler Design
Compiler Construction
What does it mean? Notes from Robert Sebesta Programming Languages
Context-free Languages
Syntax Analysis Part III
Bison: Parser Generator
Compiler Design 4. Language Grammars
Syntax Analysis Part III
Syntax Analysis Part III
Chapter 2: A Simple One Pass Compiler
Syntax Analysis Part III
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Systems Programming & Operating Systems Unit – III
Faculty of Computer Science and Information System
Presentation transcript:

2-1. LEX & YACC

2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling technique –Semantics  Yacc

3 Syntax Definition  Grammar : describing the hierarchical structures of many programming language structures  stmt  if (expr) stmt else stmt  A context-free grammar –A set of tokens, known as terminal symbols –A set of nonterminals –A set of productions left-side -> right side (left-side: nonterminal, right side: a sequence of tokens and/or nonterminals –A designation of one of the nonterminals as the start symbol

4 An Example expression  expression + term | term term  term * factor | factor factor  (expression) | identifier  실제 예를 들어서 설명함.

5 Parse Trees  정의 –The root is labeled by the start symbol –Each leaf is labeled by a token or by ∈ –Each interior node is labeled by a nonterminal When A is a node then X1, …, Xn are children if A -> X1 … Xn is a production  앞의 예를 이용하여 설명

6 Ambiguity, Associativity, Precedence  Ambiguity of a grammar –A grammar having more than one parse tree generating a given string of tokens –E  E+E | E*E| id  Associativity of operators – between operators with same precedence – left/right associative  Precedence of operators – between different operators  예로서 설명  +, *, **

7 Syntax-directed translation ProductionSemantic Rule expr -> expr 1 + term expr -> expr 1 – term expr -> term term -> 0 term -> 1 … term -> 9 expr.t := expr 1.t || term.t || ‘ + ’ expr.t := expr 1.t || term.t || ‘ - ’ expr.t := term.t term.t := ‘ 0 ’ term.t := ‘ 1 ’ … term.t := ‘ 9 ’ Figure 2.5 syntax-directed definition for infix to postfix translation

8 Figure 2.6 Attribute values at nodes In parse tree Figure 2.8 Annotated parse tree for begin west south

9 Lex is a lexical analyzer generator and Yacc is a parser generator. Lex programs recognize regular expressions and yacc generates parsers that accept a large class of context-free grammars. Below is a figure which shows how lex and yacc can be combined to perform the "lexical analysis" phase of a compiler

10 Lex - specify a set of lexical rules to lex in a source file. The general format of the source file is: {definitions} % {rules} % {programmer subroutines} digit [0-9] digits {digit}+ whitespace [ \t\n] % "[" { printf("OPEN_BRAC\n");} "]" { printf("CLOSE_BRAC\n");} "+" { printf("ADDOP\n");} "*" { printf("MULTOP\n");} {digits} { printf("NUMBER = %s\n", yytext);} whitespace ;

11 lex foo.lex cc lex.yy.c -ll $ a.out /* a.out expects it's input from standard input */ input: [ * 3 ] output: OPEN_BRAC NUMBER = 1 ADDOP NUMBER = 2 MULTOP NUMBER = 3 CLOSE_BRAC

12 Yacc -The parser gets it's input (a sequence of tokens) from the lexical analyzer (created using lex). -The format of grammar rules {declarations} % {rules} % {programs}

13 digit [0-9] digits {digit}+ whitespace [ \t\n] % "[" { return (OPEN_BRAC);} "]" { return (CLOSE_BRAC);} "+" { return (ADDOP); } "*" { return (MULTOP); } {digits} { yylval = atoi(yytext); return (NUMBER); } whitespace ;

14 %start mystartsymbol %token ADDOP MULTOP NUMBER OPEN_BRAC CLOSE_BRAC %left ADDOP %left MULTOP % mystartsymbol : expr { printf("the value of the expression is %d\n", $1);} expr : OPEN_BRAC expr CLOSE_BRAC { $$ = $2; } | expr ADDOP expr { $$ = $1 + $3 ;} | expr MULTOP expr { $$ = $1 * $3 ;} | NUMBER { $$ = $1; } ; % /* start of programs */ #include #include "lex.yy.c" main() { return yyparse(); } yyerror(char *s) { fprintf(stderr,"%s\n",s); }

15 Shell Program ::: echo lex expr.lex lex expr.lex echo yacc expr.yacc yacc expr.yacc echo cc y.tab.c -ll cc y.tab.c -ll

16 사이트  PCYACCPCYACC 9.0 is a complete language development environment that generates C, C#, C++, Java, Delphi, and VBS source code from input Language Description Grammars for building Assemblers, Compilers, Interpreters, Browsers, Page Description Languages, Language Translators, Syntax Directed Editors, Language Validators, Natural Language Processors, Expert System Shells, and Query Languages. The PCYACC Tool-Kit includes PCLEX, Visual Debugging Tools, Object- Oriented Class Library's, and Pre-Written "Drop-In" Language engines for virtually every computer language in the world