An Introduction to LEX and YACC. SYSC Programming Languages

Similar documents
Introduction to Lex & Yacc. (flex & bison)

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

Syntax Analysis Part IV

LECTURE 11. Semantic Analysis and Yacc

Lexical and Parser Tools

Using an LALR(1) Parser Generator

CSCI Compiler Design

Chapter 3 -- Scanner (Lexical Analyzer)

Flex and lexical analysis

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:

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

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

Yacc: A Syntactic Analysers Generator

Lexical and Syntax Analysis

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

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

PRACTICAL CLASS: Flex & Bison

Yacc Yet Another Compiler Compiler

CS143 Handout 04 Summer 2011 June 22, 2011 flex In A Nutshell

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

Compiler Lab. Introduction to tools Lex and Yacc

CSE302: Compiler Design

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

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

Gechstudentszone.wordpress.com

Principles of Programming Languages

Etienne Bernard eb/textes/minimanlexyacc-english.html

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

Flex and lexical analysis. October 25, 2016

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

Edited by Himanshu Mittal. Lexical Analysis Phase

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

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

Automatic Scanning and Parsing using LEX and YACC

TDDD55 - Compilers and Interpreters Lesson 3

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

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Handout 7, Lex (5/30/2001)

Preparing for the ACW Languages & Compilers

TDDD55- Compilers and Interpreters Lesson 3

2 Input and Output The input of your program is any file with text. The output of your program will be a description of the strings that the program r

Marcello Bersani Ed. 22, via Golgi 42, 3 piano 3769

Applications of Context-Free Grammars (CFG)

TDDD55- Compilers and Interpreters Lesson 2

Parsing How parser works?

Syntax-Directed Translation

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

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

Program Development Tools. Lexical Analyzers. Lexical Analysis Terms. Attributes for Tokens

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

Compil M1 : Front-End

An introduction to Flex

Syntax-Directed Translation. Introduction

CSC 467 Lecture 3: Regular Expressions

LECTURE 6 Scanning Part 2

Lexical and Syntax Analysis

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

Compiler Construction

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

Compiler Construction

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

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

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

COMPILER CONSTRUCTION Seminar 02 TDDB44

Yacc: Yet Another Compiler-Compiler

LAB MANUAL OF COMPILER DESIGN. Department of Electronics & Computer Engg. Dronacharya College Of Engineering Khentawas, Gurgaon

Chapter 3 Lexical Analysis

CS 297 Report. By Yan Yao

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

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.

Compiler course. Chapter 3 Lexical Analysis

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

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

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

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

The structure of a compiler

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

Programming Assignment II

cmps104a 2002q4 Assignment 2 Lexical Analyzer page 1

Syntax Analysis Part VIII

Gechstudentszone.wordpress.com

Compiler I: Syntax Analysis

Programming Project II

Type 3 languages. Regular grammars Finite automata. Regular expressions. Deterministic Nondeterministic. a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 )

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

PSLR(1): Pseudo-Scannerless Minimal LR(1) for the Deterministic Parsing of Composite Languages

Lex Spec Example. Int installid() {/* code to put id lexeme into string table*/}

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

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

Figure 2.1: Role of Lexical Analyzer

Programming Assignment I Due Thursday, October 9, 2008 at 11:59pm

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

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

Parsing and Pattern Recognition

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

CS 403: Scanning and Parsing

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

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

Compiler construction in4020 lecture 5

Transcription:

An Introduction to LEX and YACC SYSC-3101 1 Programming Languages

CONTENTS CONTENTS Contents 1 General Structure 3 2 Lex - A lexical analyzer 4 3 Yacc - Yet another compiler compiler 10 4 Main Program 17 SYSC-3101 2 Programming Languages

1 GENERAL STRUCTURE 1 General Structure Program will consist of three parts: 1. lexical analyzer: scan.l 2. parser: gram.y 3. Everything else : main.c SYSC-3101 3 Programming Languages

2 LEX - A LEXICAL ANALYZER 2 Lex - A lexical analyzer %{ /* C includes */ }% /* Definitions */ %% /* Rules */ %% /* user subroutines */ Figure 1: Lex program structure SYSC-3101 4 Programming Languages

Lex Definitions 2 LEX - A LEXICAL ANALYZER Lex Definitions Table with two columns: 1. regular expressions 2. actions ie: integer printf("found keyword INT"); If action has more than one statement, enclose it within {} SYSC-3101 5 Programming Languages

Regular Expressions 2 LEX - A LEXICAL ANALYZER Regular Expressions text characters: a - z, 0-9, space... \n : newline. \t : tab. operators: " \ [ ] ˆ -?. * + ( ) $ / { } % < > "..." : treat... as text characters (useful for spaces). \ : treat next character as text character.. : match anything. SYSC-3101 6 Programming Languages

Regular Expressions 2 LEX - A LEXICAL ANALYZER operators (cont): [...] : match anything within []? : match zero or one time, eg: ab?c ac, abc * : match zero or more times, eg: ab*c ac, abc, abbc... + : match one or more times, eg: ab+c abc, abbc... (...) : group..., eg: (ab)+ ab, abab... : alternation, eg ab cd ab, cd {n,m} : repitition, eg a{1,3} a, aa, aaa {defn} : substitute defn (from first section). SYSC-3101 7 Programming Languages

Actions 2 LEX - A LEXICAL ANALYZER Actions ; Null action. ECHO; printf("%s", yytext); {...} Multi-statement action. return yytext; send contents of yytext to the parser. yytext : C-String of matched characters (Make a copy if neccessary!) yylen : Length of the matched characters. SYSC-3101 8 Programming Languages

Actions 2 LEX - A LEXICAL ANALYZER Figure 2: LEX Template %{ /* -*- c -*- */ #include <stdio.h> #include "gram.tab.h" %} extern YYSTYPE yylval; %% [0-9] { yylval.anint = atoi((char *)&yytext[0]); return INTEGER; }. return *yytext; %% SYSC-3101 9 Programming Languages

3 YACC - YET ANOTHER COMPILER COMPILER 3 Yacc - Yet another compiler compiler %{ /* C includes */ }% /* Other Declarations */ %% /* Rules */ %% /* user subroutines */ Figure 3: Yacc program structure SYSC-3101 10 Programming Languages

YACC Rules 3 YACC - YET ANOTHER COMPILER COMPILER YACC Rules A grammar rule has the following form: A : BODY ; A is a non-terminal name (LHS). BODY consists of names, literals, and actions. (RHS) literals are enclosed in quotes, eg: + \n newline. \ single quote. SYSC-3101 11 Programming Languages

YACC Rules 3 YACC - YET ANOTHER COMPILER COMPILER The rules: A : B ; A : C D ; A : E F G ; can be specified as: A : B C D E F G ; SYSC-3101 12 Programming Languages

YACC Rules 3 YACC - YET ANOTHER COMPILER COMPILER Names representing tokens must be declared; this is most simply done by writing %token name1 name2... Define name1, name2,... in the declarations section. Every name not defined in the declarations section is assumed to represent a nonterminal symbol. Every nonterminal symbol must appear on the left side of at least one rule. SYSC-3101 13 Programming Languages

Actions 3 YACC - YET ANOTHER COMPILER COMPILER Actions the user may associate actions to be performed each time the rule is recognized in the input process, eg: XXX : YYY ZZZ { printf("a message\n"); } ; $ is special! $n psuedo-variables which refer to the values returned by the components of the right hand side of the rules. $$ The value returned by the left-hand side of a rule. expr : ( expr ) { $$ = $2 ; } Default return type is integer. SYSC-3101 14 Programming Languages

Declarations 3 YACC - YET ANOTHER COMPILER COMPILER Declarations %token : declares ALL terminals which are not literals. %type : declares return value type for non-terminals. %union : declares other return types. the type typedef union { body of union... } YYSTYPE; is generated and must be included into the lex source so that types can be associated with tokens. SYSC-3101 15 Programming Languages

Declarations 3 YACC - YET ANOTHER COMPILER COMPILER Figure 4: YACC Template %{ #include <stdio.h> %} %token <anint> INTEGER %type <anint> S E %union { int anint; } %% S : E { printf( "Result is %d\n", $1 ); } ; %% yyerror( char * s ) { fprintf( stderr, "%s\n", s ); } SYSC-3101 16 Programming Languages

4 MAIN PROGRAM 4 Main Program Figure 5: Main template #include <stdio.h> #include <stdlib.h> extern int yyerror(), yylex(); #define YYDEBUG 1 #include "gram.tab.c" #include "lex.yy.c" main() { /* yydebug = 1; */ yyparse(); } SYSC-3101 17 Programming Languages

4 MAIN PROGRAM Figure 6: Running the example Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\DJGPP\etc\SYSC-3 1>bison -d gram.y C:\DJGPP\etc\SYSC-3 1>flex scan.l C:\DJGPP\etc\SYSC-3 1>gcc main.c C:\DJGPP\etc\SYSC-3 1>echo 1 + 2 a.exe Result is 3 C:\DJGPP\etc\SYSC-3 1> SYSC-3101 18 Programming Languages

4 MAIN PROGRAM Figure 7: Running the example using Visual C++ Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. E:\YaccLex>bison -d gram.y E:\YaccLex>flex scan.l E:\YaccLex>cl main.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.88 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. E:\YaccLex>main.exe 2 + 1 Result is 3 SYSC-3101 19 Programming Languages