SlideShare a Scribd company logo
PRACTICA 08 : ANALISIS SINTACTICA CON BISON
EJEMPLO 2: CALCULADORA DE SUMAS Y RESTAS.
Calculadora.l
Calculadora.y
%{
#include "calculadora.tab.h"
%}
NUM [0-9]+
%%
{NUM} { yylval = atoi(yytext);
return (ENTERO); }
"+"|"-" { return (yytext[0]); }
"n" { return (yytext[0]); }
. ;
%%
%{
#define YYSTYPE int
#include <math.h>
#include <stdio.h>
%}
/* Declaraciones de BISON*/
%token ENTERO
%left '-' '+'
/* Gramatica*/
%%
input : /*Cadena Vacia*/
| input line
;
line : 'n'
| exp 'n' { printf ("t%dn", $1); }
;
exp : ENTERO { $$ = $1; }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
;
%%
int main() {
yyparse();
}
yyerror (char *s) {
printf ("%sn", s);
Las pruebas del programa se ven en la captura siguiente:
}
int yywrap() {
return 1;
}
EJEMPLO 3: CALCULADORA CON EXPRESIONES ARITMETICAS COMPLEJAS
Ejemplo3.l
%{
#include "ejemplo3.tab.h"
#include <stdlib.h>
#include <stdio.h>
%}
white [ t]+
digit [0-9]
integer {digit}+
%%
{white} { /* Ignoramos espacios en blanco */ }
"exit"|"quit"|"bye" {printf("Terminando
programan");exit(0);}
{integer} {
yylval.dval=atof(yytext);
return(NUMBER);
}
"+" return(SUMA);
"-" return(RESTA);
"*" return(MULTI);
"/" return(DIVIDE);
"^" return(EXPO);
"(" return(IZQ_PAREN);
")" return(DERE_PAREN);
"n" return(END);
%%
Ejemplo3.y
%{
#include <stdio.h>
#include <math.h>
%}
%union {
double dval;
}
%token <dval> NUMBER
%token SUMA RESTA MULTI DIVIDE EXPO
%token IZQ_PAREN DERE_PAREN
%token END
%left SUMA RESTA
%left MULTI DIVIDE
%left NEG
%right EXPO
%type <dval> Expression
%start Input
%%
Input : Line
| Input Line
;
Line : END
| Expression END { printf("Result: %fn",$1); }
;
Expression : NUMBER { $$=$1; }
| Expression SUMA Expression { $$=$1+$3; }
| Expression RESTA Expression { $$=$1-$3; }
| Expression MULTI Expression { $$=$1*$3; }
| Expression DIVIDE Expression { $$=$1/$3; }
| RESTA Expression %prec NEG { $$=-$2; }
| Expression EXPO Expression { $$=pow($1,$3); }
| IZQ_PAREN Expression DERE_PAREN { $$=$2; }
;
%%
int yyerror(char *s) {
printf("%sn",s);
}
int main(void) {
yyparse();
}
Las pruebas del programa se ven en la captura siguiente:

More Related Content

What's hot

Func menu mostrar.c
Func menu mostrar.cFunc menu mostrar.c
Func menu mostrar.calbertinous
 
Para la suma y la multiplicacion de 2
Para la suma y la multiplicacion de 2Para la suma y la multiplicacion de 2
Para la suma y la multiplicacion de 2fabiorodriguez123
 
PROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
PROBLEMAS DE PROGRAMACION 2 por jordan puente quintoPROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
PROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
Jordan Puente
 
listing output program C
listing output program Clisting output program C
listing output program C
AdjievanGestu
 
Ejercicios.
Ejercicios. Ejercicios.
Ejercicios.
Jose Dani
 
(Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ (Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ Eli Diaz
 
TUGAS BAHASA C
TUGAS BAHASA CTUGAS BAHASA C
TUGAS BAHASA C
Hastih Leo
 
Pruebabfs
PruebabfsPruebabfs
PruebabfsJorge
 
C Program : Sorting : Bubble,
C Program : Sorting : Bubble, C Program : Sorting : Bubble,
C Program : Sorting : Bubble, Meita Jayani
 
Docuemnto 6
Docuemnto 6Docuemnto 6
Docuemnto 6
Diana597594
 
Structure of c program | CS8251 | Programming in c | Learn Hub
Structure of c program | CS8251 | Programming in c | Learn HubStructure of c program | CS8251 | Programming in c | Learn Hub
Structure of c program | CS8251 | Programming in c | Learn Hub
Learn Hub
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
Dr. Loganathan R
 
Perhitungan Dua Bilangan dengan Java NetBeans
Perhitungan Dua Bilangan dengan Java NetBeansPerhitungan Dua Bilangan dengan Java NetBeans
Perhitungan Dua Bilangan dengan Java NetBeansAldila Putri
 

What's hot (20)

Fibonacci
FibonacciFibonacci
Fibonacci
 
Func menu mostrar.c
Func menu mostrar.cFunc menu mostrar.c
Func menu mostrar.c
 
Include
IncludeInclude
Include
 
質數的判斷
質數的判斷質數的判斷
質數的判斷
 
Para la suma y la multiplicacion de 2
Para la suma y la multiplicacion de 2Para la suma y la multiplicacion de 2
Para la suma y la multiplicacion de 2
 
PROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
PROBLEMAS DE PROGRAMACION 2 por jordan puente quintoPROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
PROBLEMAS DE PROGRAMACION 2 por jordan puente quinto
 
listing output program C
listing output program Clisting output program C
listing output program C
 
Ejercicios.
Ejercicios. Ejercicios.
Ejercicios.
 
(Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ (Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++
 
TUGAS BAHASA C
TUGAS BAHASA CTUGAS BAHASA C
TUGAS BAHASA C
 
Pruebabfs
PruebabfsPruebabfs
Pruebabfs
 
Power%20compounding
Power%20compoundingPower%20compounding
Power%20compounding
 
Numeros primos
Numeros primosNumeros primos
Numeros primos
 
MCD
MCDMCD
MCD
 
C Program : Sorting : Bubble,
C Program : Sorting : Bubble, C Program : Sorting : Bubble,
C Program : Sorting : Bubble,
 
Docuemnto 6
Docuemnto 6Docuemnto 6
Docuemnto 6
 
B.f.s
B.f.sB.f.s
B.f.s
 
Structure of c program | CS8251 | Programming in c | Learn Hub
Structure of c program | CS8251 | Programming in c | Learn HubStructure of c program | CS8251 | Programming in c | Learn Hub
Structure of c program | CS8251 | Programming in c | Learn Hub
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
 
Perhitungan Dua Bilangan dengan Java NetBeans
Perhitungan Dua Bilangan dengan Java NetBeansPerhitungan Dua Bilangan dengan Java NetBeans
Perhitungan Dua Bilangan dengan Java NetBeans
 

Practica 08 bison

  • 1. PRACTICA 08 : ANALISIS SINTACTICA CON BISON EJEMPLO 2: CALCULADORA DE SUMAS Y RESTAS. Calculadora.l Calculadora.y %{ #include "calculadora.tab.h" %} NUM [0-9]+ %% {NUM} { yylval = atoi(yytext); return (ENTERO); } "+"|"-" { return (yytext[0]); } "n" { return (yytext[0]); } . ; %% %{ #define YYSTYPE int #include <math.h> #include <stdio.h> %} /* Declaraciones de BISON*/ %token ENTERO %left '-' '+' /* Gramatica*/ %% input : /*Cadena Vacia*/ | input line ; line : 'n' | exp 'n' { printf ("t%dn", $1); } ; exp : ENTERO { $$ = $1; } | exp '+' exp { $$ = $1 + $3; } | exp '-' exp { $$ = $1 - $3; } ; %% int main() { yyparse(); } yyerror (char *s) { printf ("%sn", s);
  • 2. Las pruebas del programa se ven en la captura siguiente: } int yywrap() { return 1; }
  • 3. EJEMPLO 3: CALCULADORA CON EXPRESIONES ARITMETICAS COMPLEJAS Ejemplo3.l %{ #include "ejemplo3.tab.h" #include <stdlib.h> #include <stdio.h> %} white [ t]+ digit [0-9] integer {digit}+ %% {white} { /* Ignoramos espacios en blanco */ } "exit"|"quit"|"bye" {printf("Terminando programan");exit(0);} {integer} { yylval.dval=atof(yytext); return(NUMBER); } "+" return(SUMA); "-" return(RESTA); "*" return(MULTI); "/" return(DIVIDE); "^" return(EXPO); "(" return(IZQ_PAREN); ")" return(DERE_PAREN); "n" return(END); %% Ejemplo3.y %{ #include <stdio.h> #include <math.h> %} %union { double dval; } %token <dval> NUMBER %token SUMA RESTA MULTI DIVIDE EXPO %token IZQ_PAREN DERE_PAREN %token END %left SUMA RESTA %left MULTI DIVIDE %left NEG %right EXPO %type <dval> Expression %start Input %% Input : Line | Input Line ; Line : END
  • 4. | Expression END { printf("Result: %fn",$1); } ; Expression : NUMBER { $$=$1; } | Expression SUMA Expression { $$=$1+$3; } | Expression RESTA Expression { $$=$1-$3; } | Expression MULTI Expression { $$=$1*$3; } | Expression DIVIDE Expression { $$=$1/$3; } | RESTA Expression %prec NEG { $$=-$2; } | Expression EXPO Expression { $$=pow($1,$3); } | IZQ_PAREN Expression DERE_PAREN { $$=$2; } ; %% int yyerror(char *s) { printf("%sn",s); } int main(void) { yyparse(); } Las pruebas del programa se ven en la captura siguiente: