jitters: parse: add parsing module
This commit is contained in:
parent
34c9ad0189
commit
f1a6435969
|
@ -5,4 +5,10 @@ EXTRA_DIST = \
|
|||
bootstrap \
|
||||
$(NULL)
|
||||
|
||||
# Initialise those variables for use in included files
|
||||
BUILT_SOURCES =
|
||||
noinst_LIBRARIES =
|
||||
# We want to remove those files when `clean` rule is being executed
|
||||
CLEANFILES = $(BUILT_SOURCES)
|
||||
|
||||
include %D%/src/local.am
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "parse/parse-jitters.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("Hello World!");
|
||||
int res;
|
||||
yydebug = getenv("PARSE") != NULL;
|
||||
if (yyparse(&res) == 0) {
|
||||
printf("%d\n", res);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,3 +7,11 @@ jitters_SOURCES = \
|
|||
# Add warnings
|
||||
jitters_CFLAGS = $(WARN_CFLAGS)
|
||||
jitters_LDFLAGS = $(WARN_LDFLAGS)
|
||||
|
||||
# Make includes cleaner
|
||||
AM_CPPFLAGS = -I$(top_builddir)/src -I$(srcdir)
|
||||
|
||||
# Initialise those variables for use in included files
|
||||
jitters_LDADD =
|
||||
|
||||
include %D%/parse/local.am
|
||||
|
|
27
src/parse/local.am
Normal file
27
src/parse/local.am
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Do not warn about YACC compatibility, but do output token codes
|
||||
AM_YFLAGS = -Wno-yacc -d
|
||||
|
||||
# Add parsing library to `jitters`
|
||||
jitters_LDADD += \
|
||||
libparse.a \
|
||||
$(NULL)
|
||||
# Declare library as non-installable
|
||||
noinst_LIBRARIES += \
|
||||
libparse.a \
|
||||
$(NULL)
|
||||
# Add Flex/Bison sources to parsing library
|
||||
libparse_a_SOURCES = \
|
||||
%D%/parse-jitters.y \
|
||||
%D%/scan-jitters.l \
|
||||
$(NULL)
|
||||
# Mark their output files as being built by Flex/Bison
|
||||
BUILT_SOURCES += \
|
||||
%D%/parse-jitters.c \
|
||||
%D%/parse-jitters.h \
|
||||
%D%/scan-jitters.c \
|
||||
$(NULL)
|
||||
|
||||
# Remove problematic warnings from generated files
|
||||
libparse_a_CFLAGS = \
|
||||
-Wno-switch-default \
|
||||
$(NULL)
|
64
src/parse/parse-jitters.y
Normal file
64
src/parse/parse-jitters.y
Normal file
|
@ -0,0 +1,64 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
|
||||
int yylex(void);
|
||||
void yyerror(int *unused,char const *s);
|
||||
%}
|
||||
|
||||
%define api.value.type {int}
|
||||
%define api.token.prefix {TOK_}
|
||||
%define parse.error verbose
|
||||
%define parse.trace true
|
||||
%parse-param {int *result}
|
||||
%locations
|
||||
|
||||
%token EOF 0 "end-of-file"
|
||||
%token NUM "number"
|
||||
%token
|
||||
PLUS "+"
|
||||
MINUS "-"
|
||||
TIMES "*"
|
||||
DIVIDE "/"
|
||||
LPAREN "("
|
||||
RPAREN ")"
|
||||
|
||||
%left PLUS MINUS
|
||||
%left TIMES DIVIDE
|
||||
%precedence NEG
|
||||
|
||||
%%
|
||||
|
||||
input:
|
||||
exp EOF
|
||||
{ *result = $1; }
|
||||
;
|
||||
|
||||
exp:
|
||||
NUM
|
||||
{ $$ = $1; }
|
||||
|
||||
| exp PLUS exp
|
||||
{ $$ = $1 + $3; }
|
||||
| exp MINUS exp
|
||||
{ $$ = $1 - $3; }
|
||||
| exp TIMES exp
|
||||
{ $$ = $1 * $3; }
|
||||
| exp DIVIDE exp
|
||||
{ $$ = $1 / $3; }
|
||||
|
||||
| PLUS exp %prec NEG
|
||||
{ $$ = $2; }
|
||||
| MINUS exp %prec NEG
|
||||
{ $$ = -$2; }
|
||||
|
||||
| LPAREN exp RPAREN
|
||||
{ $$ = $2; }
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
void yyerror(int *unused, char const *s)
|
||||
{
|
||||
unused = unused; // Unused
|
||||
fprintf(stderr, "%s\n", s);
|
||||
}
|
45
src/parse/scan-jitters.l
Normal file
45
src/parse/scan-jitters.l
Normal file
|
@ -0,0 +1,45 @@
|
|||
%{
|
||||
#include "parse-jitters.h"
|
||||
|
||||
#define YY_USER_ACTION \
|
||||
do { \
|
||||
yylloc.first_line = yylloc.last_line; \
|
||||
yylloc.first_column = yylloc.last_column; \
|
||||
for (size_t i = 0; yytext[i] != '\0'; ++i) { \
|
||||
if (yytext[i] == '\n') { \
|
||||
yylloc.last_line++; \
|
||||
yylloc.last_column = 0; \
|
||||
} \
|
||||
else { \
|
||||
yylloc.last_column++; \
|
||||
} \
|
||||
} \
|
||||
} while (0);
|
||||
%}
|
||||
|
||||
/* Assume single input file */
|
||||
%option noyywrap
|
||||
/* Let Flex track line number */
|
||||
%option yylineno
|
||||
|
||||
/* Regex shorthands */
|
||||
blank [ \t\n]+
|
||||
int [0-9]+
|
||||
|
||||
%%
|
||||
|
||||
"+" return TOK_PLUS;
|
||||
"-" return TOK_MINUS;
|
||||
"*" return TOK_TIMES;
|
||||
"/" return TOK_DIVIDE;
|
||||
"(" return TOK_LPAREN;
|
||||
")" return TOK_RPAREN;
|
||||
|
||||
{int} {
|
||||
yylval = atoi(yytext);
|
||||
return TOK_NUM;
|
||||
}
|
||||
|
||||
{blank} { /* Do noting */ }
|
||||
|
||||
%%
|
Loading…
Reference in a new issue