Lex.

Slides:



Advertisements
Similar presentations
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Advertisements

Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler.
CS 310 – Fall 2006 Pacific University CS310 Lex & Yacc Today’s reference: UNIX Programming Tools: lex & yacc by: Levine, Mason, Brown Chapter 1, 2, 3 November.
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
Compiler Tools Lex/Yacc – Flex & Bison.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Scanning & Parsing with Lex and YACC
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Compiler Tools Lex/Yacc – Flex & Bison. Compiler Front End (from Engineering a Compiler) Scanner (Lexical Analyzer) Maps stream of characters into words.
Introduction to Lex Ying-Hung Jiang
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
1 Using Lex. 2 Introduction When you write a lex specification, you create a set of patterns which lex matches against the input. Each time one of the.
1 Using Lex. Flex – Lexical Analyzer Generator A language for specifying lexical analyzers Flex compilerlex.yy.clang.l C compiler -lfl a.outlex.yy.c a.outtokenssource.
COMPILERS AND INTERPRETERS Lesson 3 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker Department.
Introduction to Lex Fan Wu
Introduction to YACC Panfeng Xue
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
Practical 1-LEX Implementation
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
Syntactic Analysis Tools
Lecture 7. YACC YACC can parse input streams consisting of tokens with certain values. This clearly describes the relation YACC has with Lex, YACC has.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
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.
ICS312 LEX Set 25. LEX Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the C program.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Compiler Tools Lex/Yacc – Flex & Bison. Compiler Front End (from Engineering a Compiler) Scanner (Lexical Analyzer) Maps stream of characters into words.
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)
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.
LEX Input file: Output file: Total=-636 %{ int total = 0; %} % [0-9]+ {printf("+"); REJECT;} [+/-]?[0-9]+ {ECHO;
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
LEX SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
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.
Compiler Construction Sohail Aslam Lecture Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Yacc.
Sung-Dong Kim, School of Computer Engineering, Hansung University
Syntax Analysis Part III
Compiler Tools Lex/Yacc – Flex & Bison.
Tutorial On Lex & Yacc.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Context-free Languages
Regular Languages.
TDDD55- Compilers and Interpreters Lesson 2
Syntax Analysis Part III
Syntax Analysis Part III
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Other Issues - § 3.9 – Not Discussed
Compiler Structures 3. Lex Objectives , Semester 2,
Appendix B.1 Lex Appendix B.1 -- Lex.
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Regular Expressions and Lexical Analysis
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Lex Appendix B.1 -- Lex.
Presentation transcript:

Lex

Compiling Sequence

Lex letter(letter|digit)* start: goto state0 state0: read c if c = letter goto state1 goto state0 state1: read c if c = digit goto state1 goto state2 state2: accept string

Lex Structure example … definitions … %% … rules … … subroutines … /* match everything except newline */ . ECHO; /* match newline */ \n ECHO; int yywrap(void) { return 1; } int main(void) { yylex(); return 0;

Example %{ int yylineno; %} %% ^(.*)\n printf("%4d\t%s", ++yylineno, yytext); int main(int argc, char *argv[]) { yyin = fopen(argv[1], "r"); yylex(); fclose(yyin); } gdueck@ash lex]$ lex lino.l [gdueck@ash lex]$ ls -l total 40 -rw-r--r-- 1 gdueck fcsprofs 36053 Apr 9 10:15 lex.yy.c -rw-r--r-- 1 gdueck fcsprofs 215 Apr 9 10:13 lino.l [gdueck@ash lex]$ cc lex.yy.c total 64 -rwxr-xr-x 1 gdueck fcsprofs 20556 Apr 9 10:16 a.out

Count Identifiers digit [0-9] letter [A-Za-z] %{ int count; %} %% /* match identifier */ {letter}({letter}|{digit})* count++; . \n int yywrap(void) { return 1; } int main(void) { yylex(); printf("number of identifiers = %d\n", count); return 0;

quoted strings %{ char *yylval; #include <string.h> %} %% \"[^"\n]*["\n] { yylval = strdup(yytext+1); if (yylval[yyleng-2] != '"') warning("improperly terminated string"); else yylval[yyleng-2] = 0; printf("found '%s'\n", yylval); }

%{ char buf[100]; char *s; %} %x STRING %% \" { BEGIN STRING; s = buf; } <STRING>\\n { *s++ = '\n'; } <STRING>\\t { *s++ = '\t'; } <STRING>\\\" { *s++ = '\"'; } <STRING>\" { *s = 0; BEGIN 0; printf("found '%s'\n", buf); } <STRING>\n { printf("invalid string"); exit(1); } <STRING>. { *s++ = *yytext; }

Yacc yacc -d bas.y # create y.tab.h, y.tab.c lex bas.l # create lex.yy.c cc lex.yy.c y.tab.c -obas.exe # compile/link

calc.y %token INTEGER %% program: program expr '\n' { printf("%d\n", $2); } | ; expr: INTEGER { $$ = $1; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } int yyerror(char *s) { fprintf(stderr, "%s\n", s); return 0; } int main(void) { yyparse();

calc.l %{ #include "y.tab.h" %} %% [0-9]+ { yylval = atoi(yytext); [0-9]+ { yylval = atoi(yytext); return INTEGER; } [-+\n] return *yytext; [ \t] ; /* skip whitespace */ . yyerror("invalid character"); int yywrap(void) { return 1;