Está en la página 1de 16

MANUAL FLEX Y BISON

EN QT
UNIVERSIDAD DE SAN CARLOS DE GUATEMALA

Organización de lenguajes y Compiladores 1


%{
#include "scanner.h"
#include <iostream>
#include <QString>
extern int yylineno;
extern int columna;
extern char *yytext;
int yyerror(const char* mens)
{
std::cout <<mens<<" "<<yytext<< std::endl;
return 0;
}
%}
%union{
char STR [256];
}
%token<STR> Niden
%token<STR> Nnum

%%
S: Lista { std::cout<<"fin"<<std::endl; };
Lista: Lista K
| K;

K : Niden { std::cout<<$1<<std::endl; }
|Nnum { std::cout<<*$1<<std::endl; };

%%
%option noyywrap
%{
#include "parser.h"
#include <iostream>
#include <QString>
int columna=0;
%}
letra [a-zñA-ZÑ]
digito [0-9]
iden {letra}+
Numero {digito}+
%%
{iden} { columna=columna+strlen(yylval.STR); strcpy(yylval.STR, yytext); return Niden; }
{Numero} { columna=columna+strlen(yylval.STR); strcpy(yylval.STR, yytext); return Nnum; }
[[:blank:]] { /*Se ignoran los espacios en blanco */ }
. {
std::cout <<yytext<<"Error Lexico "<< std::endl;
}
%%
luci@luci-laptop:~$ cd EjemploQT/M2parser/
luci@luci-laptop:~/EjemploQT/M2parser$ flex --header-file=scanner.h -o scanner.cpp lexer.l
luci@luci-laptop:~/EjemploQT/M2parser$ bison -o parser.cpp --defines=parser.h parser.y
#include <stdio.h>
#include <stdlib.h>
extern int yyrestart( FILE* archivo);
extern int yyparse();

FILE* input = fopen("/home/luci/prueba.txt" , "r" );


yyrestart(input);
yyparse();

También podría gustarte