Escolar Documentos
Profissional Documentos
Cultura Documentos
Lex
Lex: O lex é um gerador de analisador
léxico. O seu propósito e transformar
uma série de símbolos em tokens.
O mesmo recebe uma especificação
num arquivo de extensão .l, contendo o
conjunto de expressões regulares que
deve reconhecer e as ações
associadas a cada token reconhecido.
Estrutura
declarações
%%
regras de tradução
%%
procedimentos auxiliares
Estrutura
Primeira parte do arquivo lex: declarações
%{
#include "calc.h"
#include <stdio.h>
#include <stdlib.h>
%}
/* Regular expressions */
/* ------------------- */
white [\t\n ]+
letter [A-Za-z]
digit10 [0-9] /* base 10 */
digit16 [0-9A-Fa-f] /* base 16 */
identifier {letter}(_|{letter}|{digit10})*
int10 {digit10}+
exponant [eE][+-]?{int10}
real {int10}("."{int10})?{exponant}?
Regular expressions
x the "x" character
. any character except \n
[xyz] either x, y or z
[^bz] any character, except b and z
[a-z] any character between a and z
[^a-z] any character, except those between a and z
R* zero R or more; R can be any regular expression
R+ one R or more
R? one or zero R (that is an optional R)
R{2,5} 2 to 5 R
R{2,} 2 R or more
R{2} exactly 2 R
{NOTION} expansion of NOTION, that as been defined above in the
file
\0 ASCII 0 character
\123 the caracter which ASCII code is 123, in octal
\x2A the caracter which ASCII code is 2A, in hexadecimal
RS R followed by S
R|S R or S
R/S R, only if followed by S
^R R, only at the beginning of a line
R$ R, only at the end of a line
<<EOF>> end of file
Estrutura
Segunda parte do arquivo lex: regras de tradução
main() Compilar:
{
yylex();
}
Exercício lex ex1.l
%{
int nline;
%}
%%
%%
int main() {
yylex();
printf("\nFim de programa de %d linha ",nline );
}
%{
Exercício lex ex2.l
int i,qt =0; int vetor[10];
%}
digit [0-9]
%%
{digit}+ { if( insere(atoi(yytext)) )
printf("\n TOKEN: Numero ");
else printf("\nCORE DUMP overflow" );}
%%
int main() {
yylex();
for(i=0; i< 10; i++)
printf("\nItem [%d] = %d ",i,vetor[i]);
printf("\nFim de programa" );
}
int insere(int x) {
if( qt < 10 ){
vetor[qt] = x; qt++; return(1);
}
else
return (0); }
Yacc
declarações
%%
regras de tradução
%%
procedimentos auxiliares
Estrutura
Compilar:
Exercício lex & yacc ex3.l
/* expecificacao (f)lex para subconjunto Pascal */
%{
#include "y.tab.h"
%}
ID [A-Za-z][A-Za-z0-9]*
%%
"begin" { return tBEGIN; }
"end" { return tEND; }
":=" { return tASSIGN; }
{ID} { return tIDENTIFIER; }
[0-9]+ { return tNUMBER; }
[ \n\t] /* ignora expacos em branco */
. { return *yytext; }
%%
Exercício lex & yacc ex3.y
/* expecificacao (y)acc para subconjunto Pascal */
%{
#include <stdio.h>
%}
%token tBEGIN
%token tEND
%token tASSIGN
%token tIDENTIFIER
%token tNUMBER
%left '+'
%left '-'
%start inicio
Exercício grammer ex3.y
%%
statement : tIDENTIFIER tASSIGN expression
| ;
inicio : tBEGIN stt tEND ;
stt : statement '.'
| statement ';' stt ;
expression : tNUMBER
| tIDENTIFIER
| expression '+' expression
| expression '-' expression
;
%%
int main () {
yyparse(); }
int yyerror(char *s) {
fprintf(stderr, "%s\n",s); }
#include "lex.yy.c"
Exercício lex & yacc ex4.l
/* expecificacao (f)lex para subconjunto Pascal */
%{
#include "y.tab.h"
%}
ID [A-Za-z][A-Za-z0-9]*
%%
"integer" { return tINTEGER; }
"var" { return tVAR; }
"program" { return tPROGRAM; }
"then" { return tTHEN; }
"if" { return tIF; }
"begin" { return tBEGIN; }
"end" { return tEND; }
":=" { return tASSIGN; }
{ID} { return tID; }
[0-9]+ { return tNUM; }
[ \n\t] /* ignora expacos em branco */
. { return *yytext; }
%%
Exercício lex & yacc ex4.y
/* expecificacao (y)acc para subconjunto Pascal */
%{
#include <stdio.h>
%}
%token tPROGRAM
%token tVAR
%token tINTEGER
%token tTHEN
%token tIF
%token tBEGIN
%token tEND
%token tASSIGN
%token tID
%token tNUM
%left '+'
%left '-'
%start inicio
Exercício grammer ex3.y
%%
inicio : tPROGRAM tID ";" blocoVar blocoPrincipal ;
blocoVar : tVAR blocoId ":" tINTEGER ";" | ;
blocoId : tID | blocoId ',' tID ;
blocoPrincipal : tBEGIN blocoCorpo tEND ;
blocoCorpo : comando | blocoCorpo comando | ;
comando : blocoIF | blocoSt ;
blocoIF : if | blocoIF if ;
if : tIF "(" blocoCond ")" tTHEN tBEGIN blocoSt tEND ;
blocoSt : statement | blocoSt ";" statement ;
blocoCond : ident condicao ident ;
ident : tNUM | tID ;
condicao : ">" | "<" ;
statement : tID tASSIGN expression | ;
expression : tNUM | tID| expression '+' expression ;
%%
#include "lex.yy.c"
int main () {
yyparse(); }
int yyerror(char *s) {
fprintf(stderr,"%s ERRO: Linha %d TOKEN: %s \n",s,yylineno,yytext);
}