Lex et Yacc, exemples introductifs



Similar documents
Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

LEX & YACC TUTORIAL. by Tom Niemann. epaperpress.com

LEX/Flex Scanner Generator

Yacc: Yet Another Compiler-Compiler

Antlr ANother TutoRiaL

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Informatica e Sistemi in Tempo Reale

csce4313 Programming Languages Scanner (pass/fail)

File Handling. What is a file?

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

A Lex Tutorial. Victor Eijkhout. July Introduction. 2 Structure of a lex file

Compiler Construction

Download at Boykma.Com

How Compilers Work. by Walter Bright. Digital Mars

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Programming Languages CIS 443

Chapter 2: Elements of Java

CUP User's Manual. Scott E. Hudson Graphics Visualization and Usability Center Georgia Institute of Technology. Table of Contents.

Compiler Construction using Flex and Bison

How to Write a Simple Makefile

Flex/Bison Tutorial. Aaron Myles Landwehr CAPSL 2/17/2012

Compilers Lexical Analysis

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Programming Project 1: Lexical Analyzer (Scanner)

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Theory of Compilation

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

FEEG Applied Programming 5 - Tutorial Session

The C Programming Language course syllabus associate level

SQLITE C/C++ TUTORIAL

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Comp151. Definitions & Declarations

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Introduction to Java

Sources: On the Web: Slides will be available on:

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

A Grammar for the C- Programming Language (Version S16) March 12, 2016

Evaluation of JFlex Scanner Generator Using Form Fields Validity Checking

CP Lab 2: Writing programs for simple arithmetic problems

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

ANTLR - Introduction. Overview of Lecture 4. ANTLR Introduction (1) ANTLR Introduction (2) Introduction

ESPResSo Summer School 2012

C Examples! Jennifer Rexford!

Compiler I: Syntax Analysis Human Thought

Compiler Construction

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

DToolsX-DWG. Version: An ActiveX DLL To Retrieve DWG/DXF information. DToolsX-DWG REFERENCE MANUAL

Fundamentals of Programming

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Tutorial on C Language Programming

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

The previous chapter provided a definition of the semantics of a programming

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Unix Scripts and Job Scheduling

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

/* File: blkcopy.c. size_t n

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Command Line Interface User Guide for Intel Server Management Software

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

The Cool Reference Manual

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Stacks. Linear data structures

Install Java Development Kit (JDK) 1.8

JavaScript: Control Statements I

Chapter 5 Functions. Introducing Functions

Simplifying Failure-Inducing Input

Scanner. tokens scanner parser IR. source code. errors

Basics of I/O Streams and File I/O

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

University of Wales Swansea. Department of Computer Science. Compilers. Course notes for module CS 218

Implementing Rexx Handlers in NetRexx/Java/Rexx

Block I Apollo Guidance Computer (AGC)

03 - Lexical Analysis

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

CS 241 Data Organization Coding Standards

So far we have considered only numeric processing, i.e. processing of numeric data represented

Member Functions of the istream Class

SOME EXCEL FORMULAS AND FUNCTIONS

Lecture 5: Java Fundamentals III

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Computer Programming Tutorial

Chapter 5. Recursion. Data Structures and Algorithms in Java

Transcription:

Lex et Yacc, exemples introductifs D. Michelucci 1 LEX 1.1 Fichier makefile exemple1 : exemple1. l e x f l e x oexemple1. c exemple1. l e x gcc o exemple1 exemple1. c l f l l c exemple1 < exemple1. input # l e x example1. l # cc l e x. yy. c o example1 l l # NOTE: I f you are using f l e x, i n s t e a d of lex, # you may have to change l l to l f l # in the c o m p i l a t i o n s c r i p t s. RedHat 6. x and SuSE need t h i s, # even when you invoke f l e x as lex! exemple2 : exemple2. l e x f l e x oexemple2. c exemple2. l e x gcc o exemple2 exemple2. c l f l l c exemple2 < exemple2. input exemple3 : exemple3. l e x f l e x oexemple3. c exemple3. l e x gcc o exemple3 exemple3. c l f l l c exemple3 < exemple3. input p1 : p2 : p1. l e x f l e x op1. c p1. l e x gcc o p1 p1. c l f l l c p2. l e x f l e x op2. c p2. l e x gcc o p2 p2. c l f l l c p2 < p3. input p3 : p3. l e x f l e x op3. c p3. l e x gcc o p3 p3. c l f l l c p3 p3. input 1.2 Fichier exemple1.lex % % stop start printf("stop command received\n") printf("start command received\n") 1

1.3 Fichier exemple1.input stop stop s t a r t s t a r t 1.4 Fichier exemple2.lex % % [0123456789]+ printf("number:%s\n", yytext) [a-za-z][a-za-z0-9]* printf("word:%s\n", yytext) 1.5 Fichier exemple3.lex Exemple de fichier en entrée : logging category lame-servers null category cname null zone "." type hint file "/etc/bind/db.root" La sortie correspondante : WORD OBRACE WORD FILENAME OBRACE WORD SEMICOLON EBRACE SEMICOLON WORD WORD OBRACE WORD SEMICOLON EBRACE SEMICOLON EBRACE SEMICOLON WORD QUOTE FILENAME QUOTE OBRACE WORD WORD SEMICOLON WORD QUOTE FILENAME QUOTE SEMICOLON EBRACE SEMICOLON Le fichier exemple3.lex correspondant : % % [a-za-z][a-za-z0-9]* printf("word ") [a-za-z0-9\/.-]+ printf("filename ") \" printf("quote ") \ printf("obrace ") \ printf("ebrace ") printf("semicolon ") \n printf("\n") [ \t]+ /* ignore whitespace */ 1.6 p2.lex % #define _UINT 1 2

#define _SINT 2 #define _REAL 3 #define _KEYWORD 4 #define _ID 5 int lines=0 % uint ([1-9][0-9]*) real ([0-9]?\.[0-9]+) id ([_a-za-z][_0-9a-za-z]*) ws ([ \t]+) ws \n lines++ int for do if then else return _KEYWORD uint return _UINT [+-]uint return _SINT id return _ID real return _REAL. printf("error: Undefined string found! (%s)\n",yytext) exit(1) int yywrap(void) return 1 int main(int argc, char *argv[]) int res if(argc>2) printf("syntax: %s [infile]\n",argv[0]) exit(1) if(argc==2) if((yyin=fopen(argv[1],"r"))==null) printf("cannot open input file %s.\n",argv[1]) exit(1) /* otherwise yyin = stdin */ while(res=yylex()) switch(res) case _UINT: printf("<unsigned int>") break case _SINT: printf("<signed int>") break case _REAL: printf("<real>") break case _KEYWORD: printf("<keyword>") break case _ID: printf("<id>") printf("[%s]\n",yytext) printf(" %d lines processed.\n",lines) fclose(yyin) return 0 1.7 p3.lex illustre yywrap, les états % #define _UINT 1 3

#define _SINT 2 #define _REAL 3 #define _KEYWORD 4 #define _ID 5 int lines=0 int filesno=0 char **filename=null % /* the state COMMENT is exclusive */ %x COMMENT uint ([1-9][0-9]*) real ([0-9]?\.[0-9]+) id ([_a-za-z][_0-9a-za-z]*) ws ([ \t]+) ws "/*" BEGIN(COMMENT) /* enter comment eating mode */ <COMMENT>"*/" BEGIN (INITIAL) /* exit comment eating mode */ <INITIAL,COMMENT>\n lines++ /* INITIAL is defined as 0 */ <COMMENT>. /* eat comments */ int for do if then else return _KEYWORD uint return _UINT [+-]uint return _SINT id return _ID real return _REAL. printf("error: Undefined string found!(%s)\n",yytext) exit(1) int yywrap(void) static int i=1 if(filesno<=0) return 1 /* no other input file */ if((yyin=fopen(filename[i],"r"))==null) printf("cannot open input file %s.\n",filename[i]) exit(1) i++ filesno-- return 0 int main(int argc, char *argv[]) int res if(argc == 1) printf("syntax: %s infile1 [infile2...]\n",argv[0]) exit(1) filesno=argc-1 /* number of input files */ filename=argv /* names of input files */ yywrap() /* opens the input first file */ while(res=yylex()) switch(res) case _UINT: printf("<unsigned int>") break case _SINT: printf("<signed int>") break case _REAL: printf("<real>") break 4

case _KEYWORD: printf("<keyword>") break case _ID: printf("<id>") printf("[%s]\n",yytext) printf(" %d lines processed.\n",lines) fclose(yyin) return 0 2 Yacc : calculatrice en notation polonaise 2.1 makefile ok : p o l i s h. y #l e x c a l c u l. l e x yacc p o l i s h. y o p o l i s h. cpp g++ o p o l i s h p o l i s h. cpp l f l l c 2.2 Fichier yacc : polish.y /* Reverse polish notation calculator. */ % #define YYSTYPE double #include <math.h> int yylex() int yyerror( char *) % %token NUM /* Grammar rules and actions follow */ input : /* empty */ input line line : \n exp \n printf ("\t%.10g\n", $1) exp : NUM $$ = $1 exp exp + $$ = $1 + $2 exp exp - $$ = $1 - $2 exp exp * $$ = $1 * $2 exp exp / $$ = $1 / $2 /* Exponentiation */ exp exp ^ $$ = pow ($1, $2) /* Unary minus */ exp n $$ = -$1 /* Lexical analyzer returns a double floating point number on the stack and the token NUM, or the ASCII character read if not a number. Skips all blanks and tabs, returns 0 for EOF. */ #include <ctype.h> int yylex () int c /* skip white space */ while ((c = getchar ()) == c == \t ) /* process numbers */ if (c ==. isdigit (c)) 5

ungetc (c, stdin) scanf ("%lf", &yylval) return NUM /* return end-of-file */ if (c == EOF) return 0 /* return single chars */ return c main () /* The Main function to make this stand-alone */ yyparse () int yyerror (char *s) /* Called by yyparse on error */ printf ("%s\n", s) Exemple d entrée : $ polish 2 1 + 3 2 3 5 * + 17 3 Calculatrice 3.1 makefile ok : c a l c u l scanner toupper p r e p r o c e s s e r c o u r s l e x y a c c. pdf c o u r s l e x y a c c 2. pdf c a l c u l : c a l c u l. l e x c a l c u l. yacc c a l c u l. cpp l e x c a l c u l. l e x yacc c a l c u l. yacc g++ o c a l c u l c a l c u l. cpp l f l l c c l e a n : rm f y. tab. c l e x. yy. c scanner : scanner. l e x l e x oscanner. c scanner. l e x gcc o scanner scanner. c l f l l c toupper : toupper. l e x l e x otoupper. c toupper. l e x gcc o toupper toupper. c l f l l c p r e p r o c e s s e r : p r e p r o c e s s e r. l e x l e x o p r e p r o c e s s e r. c p r e p r o c e s s e r. l e x gcc o p r e p r o c e s s e r p r e p r o c e s s e r. c l f l l c c o u r s l e x y a c c. pdf : c o u r s l e x y a c c. tex l a t e x c o u r s l e x y a c c dvipdf c o u r s l e x y a c c c o u r s l e x y a c c 2. pdf : c o u r s l e x y a c c 2. tex l a t e x c o u r s l e x y a c c 2 dvipdf c o u r s l e x y a c c 2 3.2 Fichier decla.h 6

#i f n d e f DECLA INCLUDED #d e f i n e DECLA INCLUDED #d e f i n e MAXNOMS 50 #d e f i n e MAXLENGTH 20 s t r u c t Table char tabnom [MAXNOMS] [MAXLENGTH] i n t tabval [MAXNOMS] i n t i n i t i a l i z e d [MAXNOMS] i n t tabnb e x t e r n Table t a b l e void i n i t t a b l e ( ) i n t g e t v a r ( char nom ) i n t new var ( char nom ) void i n i t i a l i z e ( i n t i n d i c e v a r, i n t value ) i n t g e t v a l ( i n t i n d i c e v a r ) i n t yylex ( ) i n t yyparse ( ) i n t y y e r r o r ( char s ) #e n d i f 3.3 Fichier calcul.cpp #include <s t d i o. h> #include <s t d l i b. h> #include <a s s e r t. h> #include <s t r i n g. h> #include d e c l a. h / the f i l e y. tab. c i s generated with : yacc c a l c u l. yacc / #include y. tab. c Table t a b l e void i n i t t a b l e ( ) t a b l e. tabnb=0 for ( i n t i =0 i <MAXNOMS i++) t a b l e. i n i t i a l i z e d [ i ]=0 t a b l e. tabval [ i ]= 0 i n t g e t v a r ( char nom) for ( i n t i =1 i<=t a b l e. tabnb i++) i f (! strcmp ( t a b l e. tabnom [ i ], nom ) ) r e t u r n i r e t u r n 0 i n t new var ( char nom) t a b l e. tabnb ++ a s s e r t ( t a b l e. tabnb < MAXNOMS ) s t r c p y ( t a b l e. tabnom [ t a b l e. tabnb ], nom ) t a b l e. tabval [ t a b l e. tabnb ]=0 t a b l e. i n i t i a l i z e d [ t a b l e. tabnb ]=0 r e t u r n t a b l e. tabnb void i n i t i a l i z e ( i n t indvar, i n t v a l e u r ) 7

i f ( indvar <=0 indvar>=maxnoms) p r i n t f ( i n i t i a l i z e bug : indvar=%d\n, indvar ) t a b l e. tabval [ indvar ] = v a l e u r t a b l e. i n i t i a l i z e d [ indvar ] = 1 i n t g e t v a l ( i n t indvar ) i f ( indvar <0 indvar>=maxnoms) p r i n t f ( g e t v a l bug : indvar=%d\n, indvar ) i f ( t a b l e. i n i t i a l i z e d [ indvar ]==0) p r i n t f ( a c c e s a une v a r i a b l e non i n i t i a l i s e e : %s \n, t a b l e. tabnom [ indvar ] ) r e t u r n t a b l e. tabval [ indvar ] main ( ) i n i t t a b l e ( ) yyparse ( ) 3.4 Fichier calcul.lex % #include d e c l a. h % c h i f f r e [0 9] l e t t r e [ a za Z ] q u i t t e r r e t u r n QUIT \ t \ n s e t r e t u r n DEF l e t t r e ( l e t t r e c h i f f r e ) i n t var var=g e t v a r ( yytext ) i f (! var ) var=new var ( yytext ) y y l v a l = var r e t u r n VAR [0 9]+ y y l v a l = a t o i ( yytext ) r e t u r n INT \ r e t u r n MOINS \/ r e t u r n DIV = r e t u r n EGAL + r e t u r n PLUS r e t u r n TIMES \ˆ r e t u r n PUISS ( r e t u r n LPAREN ) r e t u r n RPAREN r e t u r n PV. r e t u r n ERREUR 3.5 Fichier calcul.yacc %token INT %token FLOAT %token PLUS TIMES %token LPAREN RPAREN %token QUIT ERREUR 8

%token DIV MOINS %token PV %token PUISS %token VAR DEF EGAL %l e f t MOINS %l e f t PLUS / + basse precedence / %l e f t DIV %l e f t TIMES / moyenne precedence / %nonassoc UMINUS %r i g h t PUISS %s t a r t top / point d e n t r e e / top : expr PV $$ = $1 p r i n t f ( = %d \n, $$ ) top DEF VAR EGAL expr PV $$ = $4 i n i t i a l i z e ( $2, $4 ) p r i n t f ( = %d \n, $$ ) top e r r o r p r i n t f ( syntax e r r o r \n ) top QUIT r e t u r n 0 expr : INT $$= $1 VAR $$= g e t v a l ( $1 ) LPAREN expr RPAREN $$=$2 expr PLUS expr $$=$1 + $3 MOINS expr %prec UMINUS $$= $2 expr MOINS expr $$=$1 $3 expr DIV expr $$=$1 / $3 expr TIMES expr $$=$1 $3 expr PUISS expr i n t i i n t interm =1 for ( i =0 i<$3 i ++) interm = interm $1 $$=interm #include l e x. yy. c i n t y y e r r o r ( char s ) p r i n t f ( %s \n, s ) r e t u r n 0 $ calcul set a=1+2*3 = 7 set b= a^3^2 = 40353607 2+3*2^10 = 3074 2^10 = 1024 9