Académique Documents
Professionnel Documents
Culture Documents
Ejemplo
Ejemplo
/*
/*
/*
/*
============================================================ */
ejemplo lex-yacc1 */
============================================================ */
----------------------- lex.l ------------------------- */
definiciones regulares */
delimitadores
nulos
letra
digito
[ \t\n]
{delimitadores}+
[A-Za-z]
[0-9]
%%
{letra}({letra}|{digito})*
{nulos}
","
";"
.
{/*printf("id ");*/return(ID);}
;
{/*printf(",");*/return(',');}
{/*printf(";");*/return(';');}
{/*el reconocedor de cualquier caracter
debe ser el ultimo en la lista
atencion porque este tipo de reconocedores
puede resultar un tanto confuso*/
printf("error lexico \n");}
:lista';'
:lista ',' ID
|ID
;
%%
#include "lex.yy.c"
/*
/*
/*
/*
============================================================ */
ejemplo lex-yacc2 */
============================================================ */
----------------------- lex.l ------------------------- */
%{
/* este codigo C se incorpora a las declaraciones de lex.yy.c */
#include <stdlib.h>
%}
delimitadores
nulos
letra
numero
identificador
entero
[ \n\t]
{delimitadores}+
[a-zA-Z]
[0-9]
{letra}({letra}|{numero})*
{numero}+|-{numero}+
%%
{identificador}
"-"
{entero}
{nulos}
.
{/*printf("id ");*/
return(ID);}
{/*printf(" menos ");*/
return(OPM);}
{yylval=atoi(yytext);
return(NUM);
/*printf("%s ", yytext);*/}
;
{printf("otro caracter \n");}
{$$ = $1 - $3;
printf("%d \n", $$);}
{$$ = $1;
printf("%d \n", $$);}
;
term
: ID
| NUM
;
{$$ = 100;}
{$$ = yylval;}
%%
# include "lex.yy.c"
/* ========================================================== */
/* ejemplo lex-yacc3 primera aproximacion /*
/* ========================================================== */
/* ----------------------- lex.l ------------------------- */
%{
/* este codigo C se incorpora a las declaraciones de lex.yy.c */
%}
/* definiciones de expresiones regulares */
delim [ \t\n]
espacio {delim}+
digito [0-9]
numero {digito}+
%%
{espacio}
{numero}
{yylval.Natributo=atoi(yytext);
return(N);}
{return('+');}
"+"
%%
/* este codigo se incorpora al final de lex.yy.c
es necesatio en el caso de que al compilar no utilizemos la
directiva -ll */
yywrap()
{
return 1;
}
/* ------------------------- yacc.y -------------------------*/
%{
typedef struct resto {
int h,s;
} Rhs; /*definicion del registro semantico para R */
%}
%union {
int
int
int
int
Rhs
Eatributo;
Matributo;
Tatributo;
Natributo;
Ratributos;}
%token <Natributo> N
%type <Eatributo> E
%type <Matributo> M1
%type <Ratributos> R
%type <Matributo> M2
%type <Tatributo> T
%%
E
M1
: T M1 R
;
:/*nula*/
}
R
;
:'+' T M2 R
|/*nula*/
o>0, $$.s);}
;
M2
:/*nula*/
T
;
: N
%%
# include "lex.yy.c"
main(){
return (yyparse());
}
{$$.s = $4.s;}
{$$.s = $<Matributo>0;
printf("M2 final = %d
Rs = %d \n", $<Matribut
yyerror(char *s){
printf("error \n");
}
/*
/*
/*
/*
/*
========================================================== */
ejemplo lex-yacc3 segunda aproximacion */
========================================================== */
----------------------- lex.l ------------------------- */
igual que el de la primera aproximacion */
: T R
;
:'+' T M R
|/*nula*/
, $$.s);}
;
M
:/*nula*/
to>-2, $$);}
;
T
: N
{$$ = $2.s;}
{$$.s = $4.s;}
{$$.s = $<Tatributo>0;
printf("Rh en M = %d Rs = %d \n", $<Tatributo>0
{$$ = $<Tatributo>-2 + $<Tatributo>0;
printf("Rpadre = %d Rh en M = %d \n", $<Tatribu
{$$ = $1;
printf("numero = %d \n",$1);}
%%
# include "lex.yy.c"
main(){
return (yyparse());
}
yyerror(char *s){
printf("error \n");
}
/* ========================================================== */
/* ejemplo lex-yacc3 tercera aproximacion, la buena */
/* ========================================================== */
;
{yylval=atoi(yytext);
return(N);}
{return('+');}
"+"
%%
/* este codigo se incorpora al final de lex.yy.c
es necesatio en el caso de que al compilar no utilizemos la
directiva -ll */
yywrap()
{
return 1;
}
/* ------------------------- yacc.y -------------------------*/
%{
%}
%token N
%%
E
R
: T R
;
:'+' T M R
|/*nula*/
;
:/*nula*/
{$$ = $2;}
{$$ = $4;}
{$$ = $0;
printf("Rh en M = %d Rs = %d \n", $0, $$);}
{$$ = $-2 + $0;
printf("Rpadre = %d Rh en M = %d \n", $-2, $$);
}
T
;
: N
%%
# include "lex.yy.c"
main(){
return (yyparse());
}
yyerror(char *s){
{$$ = $1;
printf("numero = %d \n",$1);}
printf("error \n");
}
/*
/*
/*
/*
========================================================== */
ejemplo lex-yacc4 primera aproximacion */
========================================================== */
----------------------- lex.l ------------------------- */
%{
/* este codigo C se incorpora a las declaraciones globales de lex.yy.c */
%}
/* definiciones de expresiones regulares */
nulos
espacio
digito
letra
resto
ident
[ \t\n]
{nulos}+
[0-9]
[A-Za-z]
({letra}|{digito})*
{letra}{resto}
%%
/* declaraciones locales de yylex() */;
int
long
{strcpy(yylval.tipo, yytext);
/*printf("%s %s \n", yytext, yylval.tipo);*/
return(INT);}
{strcpy(yylval.tipo, yytext);
/*printf("%s %s \n", yytext, yylval.tipo);*/
return(LONG);}
{strcpy(yylval.lexema, yytext);
/*printf("%s %s \n", yytext, yylval.lexema);*/
return(ID);}
{return(',');}
{return(';');}
{return(':');}
{/*printf("espacio \n");*/}
{printf("error lexico");}
{ident}
","
";"
":"
{espacio}
.
%%
tipoTatributos Tatributos;
tipoMatributos Matributos;}
%token <tipo> LONG INT
%token <lexema> ID
%type <dir> L
%type <Tatributos> T
%type <Matributos> M
%start D
%%
D
: T M L
;
:INT
|LONG
;
:/*nula*/
;
:ID
<Matributos>0.dir);}
|L ',' ID
{$$ = $1 + $<Matributos>0.inc;
printf("%s %s %d \n" , $3, $<Matributos>0.tipo,
$1);}
|error ';' ID
000);}
;
%%
# include "lex.yy.c"
/*
/*
/*
/*
========================================================== */
ejemplo lex-yacc4 segunda aproximacion, la buena */
========================================================== */
----------------------- lex.l ------------------------- */
int dir;
char lexema[10], tipo[10];
tipoTatributos Tatributos;
tipoMatributos Matributos;}
%token <tipo> LONG INT
%token <lexema> ID
%type <dir> L
%type <Tatributos> T
%type <Matributos> M
%start D
%%
D
: T M L
;
:INT
|LONG
;
:/*nula*/
T
M
;
L
:ID
$<Matributos>0.dir);
|L ',' ID
$1);
|error ';' ID
$$ = $1 + $<Matributos>0.inc;
$<Matributos>0.dir=$$;}
{printf("error en la lista ident=%s \n", $3);
printf("%s %s %d \n", $3, $<Matributos>0.tipo, $
<Matributos>0.dir);
$$ = $<Matributos>0.dir + $<Matributos>0.inc;
$<Matributos>0.dir=$$;}
;
%%
# include "lex.yy.c"
/*
/*
/*
/*
========================================================== */
ejemplo lex-yacc5 */
========================================================== */
----------------------- lex.l ------------------------- */
nulos [ \t\n]
espacio {nulos}+
%%
{espacio}
[a-zA-Z]
"|"
"*"
"("
")"
.
;
{yylval.atributo = yytext[yyleng-1]; return(SIMBOLO);}
{return('|');}
{return('*');}
{return('(');}
{return(')');}
{printf("error lexico \n");}
%{
typedef struct{
int i, f;
}tipoestados;
%}
%union{
char
tipoestados
atributo;
estados;}
%token <atributo>SIMBOLO
%type <estados>E T F P
%%
E
:E '|' T
{$$.i=creaestado();
$$.f=creaestado();
printf("M[ %d, nada] = %d, %d \n", $$.i, $1.i, $
3.i);
printf("M[ %d, nada] = %d \n", $1.f, $$.f);
printf("M[ %d, nada] = %d \n", $3.f, $$.f);}
| T
;
T
:T F
{$$.i=$1.i; $$.f=$2.f;
printf("M[ %d, nada] = %d \n", $1.f, $2.i);}
| F
;
F
:P
| P '*'
{$$.i=creaestado();$$.f=creaestado();
printf("M[ %d , nada] = %d, %d \n", $$.i, $1.i,
$$.f);
printf("M[ %d , nada] = %d, %d \n", $1.f, $1.i,
$$.f);}
;
P
:'(' E ')'
| SIMBOLO
;
%%
#include "lex.yy.c"
int estado=0;
int creaestado(){
return ++estado;
}
{$$.i=$2.i; $$.f=$2.f;}
{$$.i=creaestado();$$.f=creaestado();
printf("M[ %d , %c ] = %d \n", $$.i, $1, $$.f);}