You are on page 1of 7

UNIVERSIDAD NACIONAL DEL ALTIPLANO

FACULTAD DE INGENIERIA MECANICA ELECTRICA,


ELECTRONICA Y SISTEMAS
ESCUELA PROFESIONAL DE INGENERIA DE SISTEMAS

COMPILADORES

TEMA:

EJERCICIO 3.5.5

RESPONSABLE DE ASIGNATURA:

FERNANDEZ CHAMBI MAYENKA

PRESENTADO POR:

ALUMNO: BRUNO EDUARDO ROJAS ALEJO


%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define BSIZE 128
#define NONE -1
#define EOS '\0'
int tokenval = NONE;
int lineno = 1;
int token;
char lexbuf[BSIZE];
int lexlen = 0;
struct entry {
char *lexptr;
int token;
};
struct entry keywords[] = {
{"select", SELECT},
{"from", FROM},
{"where", WHERE},
{NULL, 0}
};
int lexan()
{
int t;
int c;
while ((c = getchar()) == ' ' || c == '\t')
;
if (c == '\n') {
lineno++;
return (NEWLINE);
}
if (isdigit(c)) {
if (c == '0')
return (ZERO);
t = INTLITERAL;
goto gather;
}
if (isalpha(c)) {
if (c == 'i') {
if ((c = getchar()) == 'f') {
strcpy(lexbuf, "if");
return (IF);
}
else
ungetc(c, stdin);
}
t = ID;
goto gather;
}
switch (c) {
case EOF:
return (DONE);
case '=':
if ((c = getchar()) == '=') {
strcpy(lexbuf, "==");
return (EQ);
}
else {
ungetc(c, stdin);
return (ASSIGN);
}
case '<':
if ((c = getchar()) == '=') {
strcpy(lexbuf, "<=");
return (LE);
}
else {
ungetc(c, stdin);
return (LT);
}
case '>':
if ((c = getchar()) == '=') {
strcpy(lexbuf, ">=");
return (GE);
}
else {
ungetc(c, stdin);
return (GT);
}
case '!':
if ((c = getchar()) == '=') {
strcpy(lexbuf, "!=");
return (NE);
}
else {
printf("Error: %d: illegal operator %c\n", lineno, c);
exit(1);
}
case '|':
if ((c = getchar()) == '|') {
strcpy(lexbuf, "||");
return (OR);
}
else {
ungetc(c, stdin);
return (ERROR);
}
case '&':
if ((c = getchar()) == '&') {
strcpy(lexbuf, "&&");
return (AND);
}
else {
ungetc(c, stdin);
return (ERROR);
}
case '+':
return (PLUS);
case '-':
return (MINUS);
case '*':
return (MULT);
case '/':
return (DIV);
case '(':
return (LPAREN);
case ')':
return (RPAREN);
case ',':
return (COMMA);
case ';':
return (SEMICOLON);
default:
printf("Error: %d: illegal character %c\n", lineno, c);
exit(1);
}
gather:
lexbuf[lexlen++] = c;
while (1) {
c = getchar();
if (c == ' ' || c == '\t' || c == '\n') {
ungetc(c, stdin);
lexbuf[lexlen] = EOS;
return (t);
}
if (!isalnum(c)) {
printf("Error: %d: illegal character %c\n", lineno, c);
exit(1);
}
lexbuf[lexlen++] = c;
if (lexlen >= BSIZE) {
printf("Error: %d: lexeme too long\n", lineno);
exit(1);
}
}
}
int main()
{
while (1) {
token = lexan();
switch (token) {
case IF:
case WHILE:
case INT:
case RETURN:
case VOID:
case ELSE:
printf("reserved word: %s\n", lexbuf);
break;
case ID:
printf("identifier: %s\n", lexbuf);
break;
case INTLITERAL:
printf("integer literal: %s\n", lexbuf);
break;
case ASSIGN:
printf("assignment operator\n");
break;
case EQ:
printf("equality operator\n");
break;
case NE:
printf("not equal operator\n");
break;
case LT:
printf("less than operator\n");
break;
case LE:
printf("less than or equal operator\n");
break;
case GT:
printf("greater than operator\n");
break;
case GE:
printf("greater than or equal operator\n");
break;
case PLUS:
printf("addition operator\n");
break;
case MINUS:
printf("subtraction operator\n");
break;
case MULT:
printf("multiplication operator\n");
break;
case DIV:
printf("division operator\n");
break;
case LPAREN:
printf("left parenthesis\n");
break;
case RPAREN:
printf("right parenthesis\n");
break;
case LBRACE:
printf("left brace\n");
break;
case RBRACE:
printf("right brace\n");
break;
case LCURLY:
printf("left curly brace\n");
break;
case RCURLY:
printf("right curly brace\n");
break;
case COMMA:
printf("comma\n");
break;
case SEMICOLON:
printf("semicolon\n");
break;
case NEWLINE:
printf("newline\n");
break;
case DONE:
printf("End of file\n");
return (0);
}
}
}
%}
%%
select|SELECT|from|FROM|where|WHERE {printf("keyword: %s\n", yytext);}
[a-zA-Z_][a-zA-Z0-9_]* {printf("identifier: %s\n", yytext);}
[0-9]+ {printf("integer literal: %s\n", yytext);}
"==" {printf("equality operator\n");}
"!=" {printf("not equal operator\n");}
"<" {printf("less than operator\n");}
"<=" {printf("less than or equal operator\n");}
">" {printf("greater than operator\n");}
">=" {printf("greater than or equal operator\n");}
"+" {printf("addition operator\n");}
"-" {printf("subtraction operator\n");}
"*" {printf("multiplication operator\n");}
"/" {printf("division operator\n");}
"(" {printf("left parenthesis\n");}
")" {printf("right parenthesis\n");}
"," {printf("comma\n");}
";" {printf("semicolon\n");}
\n {printf("newline\n");}
. {printf("illegal character %c\n", yytext[0]);}
%%
int yywrap()
{
return (1);
}
void yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
}

You might also like