Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions labs/04/calculator_v2_linux_zoo/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
all:
yacc -d simplecalc.y
lex simplecalc.l
gcc y.tab.c lex.yy.c -o simplecalc

clean:
rm -rf simplecalc
rm -rf lex.yy.c
rm -rf y.tab.c
rm -rf y.tab.h
rm -rf y.tab.h.gch
all:
yacc -d simplecalc.y
lex simplecalc.l
gcc y.tab.c lex.yy.c -o simplecalc
clean:
rm -rf simplecalc
rm -rf lex.yy.c
rm -rf y.tab.c
rm -rf y.tab.h
rm -rf y.tab.h.gch
41 changes: 25 additions & 16 deletions labs/04/calculator_v2_linux_zoo/simplecalc.l
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
%{
#include "y.tab.h"
extern int yylval;

%}

%option noyywrap


%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[a-z] { yylval = yytext[0]; return NAME; }
[ \t] ; /* ignore whitespace */
\n return 0; /* logical EOF */
. return yytext[0];
%%
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
%}

%option noyywrap

%%
"//".* { printf("COMMENT "); return '\n'; }
"f" { printf("floatdcl "); return FLOATDCL; }
[a-eghj-oq-z] { printf("id "); yylval.sval = yytext[0]; return NAME;}
"i" { printf("intdcl "); return INTDCL; }
"=" { printf("assign "); return '=';}
[0-9]+ { printf("inum "); yylval.dval = atoi(yytext); return NUMBER;}
"+" { printf("plus "); return '+'; }
"*" { printf("times "); return '*'; }
"-" { printf("minus "); return '-'; }
"/" { printf("divided" ); return '/'; }
[0-9]+"."[0-9]+ { printf("fnum "); yylval.dval = atof(yytext); return NUMBER; }
"p" { printf("print "); return PRINT;}
[ \t] ; /* ignore whitespace */
\n { return '\n'; } /* logical EOF */
. { return yytext[0]; }
%%
93 changes: 65 additions & 28 deletions labs/04/calculator_v2_linux_zoo/simplecalc.y
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
%{
#include <stdio.h>
int yylex();
void yyerror (char const *s) {
fprintf (stderr, "%s\n", s);
}
%}


%token NAME NUMBER
%%

statement: NAME '=' expression {printf("%c = %d\n", $1,$3);}
| expression {printf("= %d\n", $1);}
;
expression: expression '+' NUMBER { $$ = $1 + $3;}
| expression '-' NUMBER { $$ = $1 - $3;}
| expression '*' NUMBER { $$ = $1 * $3;}
| '(' expression ')' {$$ = $2;}
| NUMBER {$$ = $1;}
;

%%

int main(){
yyparse();
return 0;
}
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}

double varvalue[26];
%}

%union {
double dval;
char sval;
}

%token <sval> NAME
%token <dval> NUMBER
%token FLOATDCL PRINT INTDCL
%type <dval> expression

%left '+' '-'
%left '*' '/'

%%

input:
| input '\n'
| input statement '\n'
;

statement: FLOATDCL NAME
| INTDCL NAME
| NAME '=' expression { varvalue[$1 - 'a'] = $3; }
| PRINT NAME { printf("%f\n", varvalue[$2 - 'a']); }
;

expression:
expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { $$ = $1 / $3; }
| '(' expression ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| NAME { $$ = varvalue[$1 - 'a']; }
;

%%

int main(int argc, char **argv) {
FILE *fd;

if (argc == 2) {
if (!(fd = fopen(argv[1], "r"))) {
perror("Error: ");
return -1;
}
yyset_in(fd);
yyparse();
fclose(fd);
} else {
printf("Usage: %s filename\n", argv[0]);
}
return 0;
}