Download presentation
Presentation is loading. Please wait.
Published byEmory Barker Modified over 9 years ago
1
Lex & Yacc logoLex.l logoLex.h logoLex.c logoYacc.y logoYacc.h logoYacc.c
2
Lex file (.l) %{ /* Lexical Analyzer for the Grammar: S-> aSb | Ce C-> cC | d */ #include "myYacc.h" %} % a{return TOKENA;} b{return TOKENB;} c{return TOKENC;} d{return TOKEND;} e{return TOKENE;} % C header stuff
3
Lex file (.l) %{ /* Lexical Analyzer for the Grammar: S-> aSb | Ce C-> cC | d */ #include "myYacc.h" %} % a{return TOKENA;} b{return TOKENB;} c{return TOKENC;} d{return TOKEND;} e{return TOKENE;} % Section Separators
4
Lex Flie (.l) Lex file has 3 sections separated by % Lex file has 3 sections separated by % Section 1: Header stuff including C header (between %{ and %} Section 1: Header stuff including C header (between %{ and %} Token definition and actions Token definition and actions Expressed patterns in extended Regular Expression Expressed patterns in extended Regular Expression Decide what to do with each legal token. Most common action is to store the value and return token type to the parser. Decide what to do with each legal token. Most common action is to store the value and return token type to the parser. C code stuff – could contain the main function here for testing purposes, but it’s usually in the parser. Uses default main if left blank. C code stuff – could contain the main function here for testing purposes, but it’s usually in the parser. Uses default main if left blank.
5
Lex file (.l) %{ /* Lexical Analyzer for the Grammar: S-> aSb | Ce C-> cC | d */ #include "myYacc.h" %} % a{return TOKENA;} b{return TOKENB;} c{return TOKENC;} d{return TOKEND;} e{return TOKENE;} % Token Patterns (extended R.E.) Actions in C code
6
Yacc Flie (.y) Yacc file also has 3 sections separated by % Yacc file also has 3 sections separated by % Section 1: Header stuff including C header (between %{ and %} Section 1: Header stuff including C header (between %{ and %} Syntax definition and actions Syntax definition and actions Expressed syntax in context-free grammar Expressed syntax in context-free grammar Decide what to do with each legal structure. Decide what to do with each legal structure. C code stuff –Uses default main if left blank. C code stuff –Uses default main if left blank. Must be connected to the.l file in several ways. Must be connected to the.l file in several ways.
7
Lex & Yacc logoLex.h logoLex.c logoYacc.h logoYacc.c return TokenType;
8
Lex & Yacc logoLex.h logoLex.c logoYacc.h logoYacc.c return NUMBER; 123 yylval 123 yytext
9
Lex & Yacc logoLex.l logoYacc.y yylval %{ #include "logoYacc.h“ extern char * yylval; %} FD{return FD;} BK{return BK;} 0|[1-9][0-9]* { yylval = (char *) malloc(sizeof(yytext)+1); strcpy(yylval, yytext); return NUMBER;} %{ #include #define YYTYPE char* %} %token FD %token BK %token TO %token REPEAT %token END %token '[' %token ']' %token NUMBER %token ID
10
Lex & Yacc logoLex.l logoYacc.y 123 yylval 0|[1-9][0-9]* { yylval = (char *) malloc(sizeof(yytext)+1); strcpy(yylval, yytext); return NUMBER;} % S : FD NUMBER { printf(“FD %s\n”, $2);} 123 yytext
11
Lex & Yacc logoYacc.y logoYacc.y S : FD NUMBER { printf(“FD: %s\n”, $2 } S
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.