close

Вход

Забыли?

вход по аккаунту

?

Introduction to YACC..

код для вставкиСкачать
Introduction to YACC
Panfeng Xue
pxx101020@utdallas.edu
LEX and YACC
• LEX
– Split the source file into tokens
• YACC
– Find the hierarchical structure of the program
LEX and YACC
Architecture
YACC Specifications
• Similar structure to LEX
– Declaration Section
– Translation Rules
– Supporting C/C++ code
Declaration
%%
Translation rules
%%
Supporting C/C++ code
YACC Declaration
• Declaration Section
– C/C++ Code
%{
#include <stdio.h>
#include <string.h>
#define MAXSTRINGLENGTH 128
int TotalParaNo=0;
….
%}
– YACC definition
• %token
• %start
• Others
%token TOK_constants TOK_functions
YACC Specifications
• Similar structure to LEX
– Declaration Section
– Translation Rules
– Supporting C/C++ code
Declaration
%%
Translation rules
%%
Supporting C/C++ code
YACC Rules
• The rules section represents a grammar. The
left-hand side of a production is followed by
a colon.
• Actions associated with a rule are entered in
braces.
statements:
| statement statements
{
printf(" statements founded”);
}
;
YACC Rules
• Prog -> SS
%%
programs:
statements
;
• SS -> S SS | ε
statements: /*empty*/
| statement
statements
;
Actions
• Actions: associated with a rule are entered in
braces.
• Similar with the LEX
statements:
| statement statements
{
printf(" statements founded”);
}
;
Symbol Values
• $1, $2….$n can be refer to the values associated with
symbols
• $$ refer to the value of the left
• Every symbol have a value associated with it
(including token and non-terminals)
• Default action:
– $$ = $1
statement:
identifier '+' identifier
{
$$ = $1 + $3;
}
| identifier '-' identifier
{
$$ = $1 - $3;
}
;
Actions
• Inherited Attributes
– { fn id PP }
– How to transfer the value of fn and id to PP?
• Using the stack information
• $1 designates the first term on the right-hand side.
• We can index backwards, using $0, $-1, and so on.
Symbol Types
• Declaring Symbol Types
%union {
int dval;
char *sval;
}%
……………………………
%token <dval> NUMBER
%token <sval> IDENTIFIER
%type <dval> statement
YACC Specifications
• Similar structure to LEX
– Declaration Section
– Translation Rules
– Supporting C/C++ code
Declaration
%%
Translation rules
%%
Supporting C/C++ code
C/C++ Codes
• Supporting C/C++ code
• Token
– In the Lex:
return (TOKEN)
– In the Parser:
В» %token TOKEN
В» // then TOKEN can be used in your yacc code
Feedback
• Feed Information back to the LEX
– YACC
%{
int top_layer = 1;
}%
……………………………
%%
Program:
statement { top_layer = 0;}
;
– Lex
%{
extern int top_layer;
}%
……………………………
Make
• Make file
– yacc –d FP.yacc
# create y.tab.h, y.tab.c
– lex FP.lex
# create lex.yy.c
– cc lex.yy.c y.tab.c –o FP # compile/link
How to Debug
• Add the following into your YACC file
%%
extern int yy_flex_debug;
int main(void) {
yy_flex_debug = 1;
yyparse();
}
• Add –-debug into your makefile
How to Debug
Документ
Категория
Без категории
Просмотров
6
Размер файла
270 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа