Docsity

Lexical Analyzer Generator - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Lexical Analyzer Generator - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Lex and Yacc

Lex and Yacc

 Two Compiler Writing Tools that are Utilized to easily

Specify:

 Lexical Tokens and their Order of Processing (Lex)

 Context Free Grammar for LALR(1) (Yacc)

 Both Lex and Yacc have Long History in Computing

 Lex and Yacc – Earliest Days of Unix

Minicomputers

 Flex and Bison – From GNU

 JFlex - Fast Scanner Generator for Java

 BYacc/J – Berkeley

 CUP, ANTRL, PCYACC, …

 PCLEX and PCYACC from Abacus

Format of a Lexical Specification – 3 Parts

 Declarations:

 Defs, Constants, Types, #includes, etc. that can

Occur in a C Program

 Regular Definitions (expressions)

 Translation Rules:

 Pairs of (Regular Expression, Action)

 Informs Lexical Analyzer of Action when Pattern is

Recognized

 Auxiliary Procedures:

 Designer Defined C Code

 Can Replace System Calls

Lex.y File Format:

DECLARATIONS

TRANSLATION RULES

AUXILIARY PROCEDURES

Example lex.l File

#define T_IDENTIFIER 300 #define T_INTEGER 301 #define T_REAL 302 #define T_STRING 303 #define T_ASSIGN 304 #define T_ELSE 305 #define T_IF 306 #define T_THEN 307 #define T_EQ 308 #define T_LT 309 #define T_NE 310 #define T_GE 311 #define T_GT 312 %}

letter [a-zA-Z] digit [0-9] ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*' %%

":=" {printf(" %s ", yytext);return(T_ASSIGN);} "else" {printf(" %s ", yytext);return(T_ELSE);}

User Defined Values

to Each Token (else

lex will assign)

Regular Expression

Rules for later

token definitions

Token

Definitions

What is wrong with Following?

letter [a-zA-Z] digit [0-9] ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*' %%

{id} {printf(" %s ", yytext);return(T_IDENTIFIER);} {integer} {printf(" %s ", yytext);return(T_INTEGER);} {real} {printf(" %s ", yytext);return(T_REAL);} {string} {printf(" %s ", yytext);return(T_STRING);} {comment} {/* T_COMMENT /} {ws} {/ spaces, tabs, newlines */}

":=" {printf(" %s ", yytext);return(T_ASSIGN);} "else" {printf(" %s ", yytext);return(T_ELSE);} "then" {printf(" %s ", yytext);return(T_THEN);} "<=" {printf(" %s ", yytext);return(T_EQ);} "<" {printf(" %s ", yytext);return(T_LT);} "<>" {printf(" %s ", yytext);return(T_NE);} ">=" {printf(" %s ", yytext);return(T_GE);} ">" {printf(" %s ", yytext);return(T_GT);}

%%

Other Possible Actions

":=" {return(T_ASSIGN);} "else" {return(T_ELSE);} "then" {return(T_THEN);} "<=" {yylval = T_EQ; return(T_EQ);} ... Etc.... ">" {yylval = T_GT; return(T_GT);}

{id} {yylval = install_id(); return(T_IDENTIFIER);} {integer} {yylval = install_int(); return(T_INTEGER);} {real} {yylval = install_real(); return(T_REAL);} {comment} {/* T_COMMENT /} {ws} {/ spaces, tabs, newlines */}

%% install_id() { /* A procedure to install the lexeme whose first character is pointed to by yytext and whose length is yylen into symbol table and return a pointer */ }

install_int() { /* Similar – but installs an integer lexeme into symbol table */ }

install_real() { /* Similar – but installs a real lexeme into symbol table */ } Docsity.com

Using the lex Compiler

 Important Highlights

 Unix Lex defaults with respect to:

 Single Rule size (2048 bytes)

 All Actions (20480 bytes)

 DFA States (512)

 NFA States (254)

 Command Line:

 lex myfile.l Generates lex.yy.c

 pclex myfile.l Generates myfile.c

 -v flag Includes Statistics on State Machine, etc.

Highlights Generated lex.yy.c File

define output (c) putc(c, yyout);

define input() ((( yytchar=yysptr>yysbug?U(*--yysptr); getc(yyin))==10?

yylineno++, yytchar):yytchar)==EOF?0:yytchar)

define uput() (yttchar= (c);if (yytchar==‚\n‘)yylineno--;*yysptr__=yytchar;}

FILE *yyin={stdin}, *yyout = {stdout};

yyinput () { return(input()); }

yyoutput(c) int c { output(c); }

yyunput(c) int c { upput(c); }

Compilation at Unix Command Line: lex lexfile.l (creates lex.yy.c) cc lex.yy.c –ll (include lex library)

Full lex.yy.c File

#define T_IDENTIFIER 300 #define T INTEGER 301 #define T_REAL 302 #define T STRING 303 #define T_ASSIGN 304 #define T ELSE 305 #define T_IF 306 #define T_THEN 307 #define T_EQ 308 #define T LT 309 #define T_NE 310 #define T GE 311 #define T_GT 312 #define YYNEWLINE 10 yylex ( ) { int nstr; extern int yyprevious; while((nstr - yylook()) >- 0) yyfussy: switch(nstr) { case 0: if(yywrap()) return(0); break; case 1: {printf(" %s ", yytext);return(TASSIGN);} break; case 2: {printf(" %s ", yytext);return(T_ELSE);} break; case 3: (printf(" %s ", yytext) ;return (T IF) ; } break;

Full lex.yy.c File

case 4: { #ifdef PRNTFLG printf(" %s ", yytext); #endif return(T_THEN); } break; case 5: {printf(" %s ", yytext);return(T_EQ);} break; case 6: {printf(" %s ", yytext);return(T_LT);} break; case 7: {printf(" %s ", yytext);return(T_NE);) break; case 8: {printf(" %s ", yytext);return(T_GE);} break; case 9: {printf(" %s ", yytext);return(T_GT);} break; case 10: {printf(" %s ", yytext);return(T_IDENTIFIER);} break; case 11: {printf(" %s ", yytext);return(T_INTEGER);) break; case 12: {printf(" %s ", yytext) ;return(T_REAL); } break; case 13: {printf(" %s ", yytext);return(T_STRING);}

A Pascal lex.l

#include "y.tab.h" %}

letter [a-zA-Z] digit [0-9]

ws [ \t\n]+ id [A-Za-z][A-Za-z0-9]* comment "("([^]|\n|""+[^)])""+")" integer [0-9]+/([^0-9]|"..") real [0-9]+"."[0-9]([0-9]|"E"[+-]?[0-9]+) string '([^']|'')*'

%%

":=" {return(T_ASSIGN);} ":" {return(T_COLON);} "array" {return(T_ARRAY);} "begin" {return(T_BEGIN);} "case" {return(T_CASE);} "const" {return(T_CONST);} "downto" {return(T_DOWNTO);} "do" {return(T_DO);} "else" {return(T_ELSE);} "end" {return(T_END);} "file" {return(T_FILE);} "for" {return(T_FOR);}

A Pascal lex.l

"function" {return(T_FUNCTION);} /* "goto" {return(T_GOTO);} / "if" {return(T_IF);} "label" {return(T_LABEL);} "nil" {return(T_NIL);} "not" {return(T_NOT);} "of" {return(T_OF);} / "packed" {return(T_PACKED);} / "procedure" {return(T_PROCEDURE);} "end" {return(T_END);} "program" {return(T_PROGRAM);} "record" {return(T_RECORD);} "repeat" {return(T_REPEAT);} "set" {return(T_SET);} "then" {return(T_THEN);} "to" {return(T_TO);} "type" {return(T_TYPE);} "until" {return(T_UNTIL);} "var" {return(T_VAR);} "while" {return(T_WHILE);} / "with" {return(T_WITH);} */ "+" {return(T_PLUS);} "-" {return(T_MINUS);} "or" {return(T_OR);} "and" {return(T_AND);} "div" {return(T_DIV);} "mod" {return(T_MOD);} "/" {return(T_RDIV);}

Project Part 1 – Fall 2011

 What is Latex?

 Text Processing Language

 Embed Commands into Ascii File

 Opposite of Word’s WYSIWYG

 Geared Towards Publishing – Particularly Prior to

Newer Versions of Work

 Very Powerful Text Formatting Language

 Invented by Computer Scientist Donald Knuth

 http://www-cs-faculty.stanford.edu/~uno/

 http://www-cs-

faculty.stanford.edu/~uno/abcde.html

 Famous for: The Art of Computer Programming

 http://www-cs-

faculty.stanford.edu/~uno/taocp.html

Project Part 1 Has Three Tasks

 Task 1: Oct 5: Design and implement a lexical

analyzer using the flex generator on the Linux

boxes that is able to identify all lexical tokens for

the latex subset.

 Task 2: Oct 12: Design and develop a context free

grammar (CFG) for a subset of Latex.

 Task 3: Oct 17: Calculate FIRST and FOLLOW for a

grammar provided after deliverable part 1b.