Skip to content

Instantly share code, notes, and snippets.

@funrep
Created January 7, 2014 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funrep/8299561 to your computer and use it in GitHub Desktop.
Save funrep/8299561 to your computer and use it in GitHub Desktop.
%{
#define YYSTYPE double /* data type of yacc stack */
%}
%token NUMBER
%left '+' '-' /* left associative, same precedence */
%left '*' '/' /* left assoc., higher precedence */
%left UNARYMINUS
%%
list: /* nothing */
| list '\n'
| list expr '\n' { printf("\t%.8g\n", $2); }
;
expr: NUMBER { $$ = $1; }
| '-' expr %prec UNARYMINUS { $$ = -$2; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
;
%%
/* end of grammer */
#include <stdio.h>
#include <ctype.h>
char *progname; /* for error message */
int lineno = 1;
main(argc, argv)
char *argv[];
{
progname = argv[0];
yyparse();
}
yylex()
{
int c;
while ((c=getchar()) == ' ' || c == '\t')
;
if (c == EOF)
return 0;
if (c == '.' || isdigit(c)) { /* number */
ungetc(c, stdin);
scanf("%1f", &yylval);
return NUMBER;
}
if (c == '\n')
lineno++;
return c;
}
yyerror(s) /* called for yacc syntax error */
char *s;
{
warning(s, (char *) 0);
}
warning(s, t) /* print warning message */
char *s, *t;
{
fprintf(stderr, "%s: %s", progname, s);
if (t)
fprintf(stderr, " %s", t);
fprintf(stderr, " near line %d\n", lineno);
}
@funrep
Copy link
Author

funrep commented Jan 7, 2014

$ ./hoc
-2-3
        -1.0630702e-314
2*5
        0
1/0
        inf
0/1
        0
1+1
        1.0527088e-314
^C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment