basic parser works

This commit is contained in:
overflowerror 2021-05-03 11:39:33 +02:00
parent f3fead6349
commit c355db419d
3 changed files with 83 additions and 14 deletions

View file

@ -11,12 +11,12 @@ marshaller: gen/lex.yy.c gen/y.tab.c
gen/y.tab.c gen/y.tab.h: src/parser.y
$(YACC) $(YFLAGS) $<
mv src/y.tab.c gen/
mv src/y.tab.h gen/
mv y.tab.c gen/
mv y.tab.h gen/
gen/lex.yy.c: src/scanner.l gen/y.tab.h
$(LEX) $<
mv src/lex.yy.c gen/
mv lex.yy.c gen/
clean:
rm -f gen/* $(BIN_NAME)

70
marshaller/src/parser.y Normal file
View file

@ -0,0 +1,70 @@
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(char*);
%}
%token TYPEDEF STRUCT
%token LONG DOUBLE STRING
%token POINTER
%token SEMICOLON OPEN_BRACES CLOSE_BRACES OPEN_BRACKETS CLOSE_BRACKETS
%token ID NUM
%start file
%%
file: declars
;
declars: /* empty */
| declar SEMICOLON declars
;
declar: structdef
| typedef
;
structdef: STRUCT ID structbody
;
typedef: TYPEDEF STRUCT structbody ID
| TYPEDEF STRUCT ID structbody ID
;
structbody: OPEN_BRACES structmembers CLOSE_BRACES
;
structmembers: /* empty */
| structmember SEMICOLON structmembers
;
structmember: type ID
;
type: LONG
| DOUBLE
| STRING
| STRUCT ID
| ID
| type POINTER
;
%%
int main(void) {
yyparse();
return 0;
}
void yyerror(char* s) {
extern int yylineno;
fprintf(stderr, "%s (line %d)\n", s, yylineno);
exit(2);
}

View file

@ -5,19 +5,17 @@ dec [0-9]+
hex [0-9a-fA-F]+H
number {dec}|{hex}
int_types "short"|"byte"|"int"|"long"|"long long"
int_types "long long"|"long"|"int"|"byte"|"short"
unsigned_int_types "unsigend "{int_types}
signed_int_types "signed "{int_types}
stdint_types_u "uint8_t"|"uint16_t"|"uint32_t"|"uint64_t"
stdint_types_s "int8_t"|"int16_t"|"int32_t"|"int64_t"
integer int_types|unsigned_int_types|signed_int_types|stdint_types_u|stdint_types_s
int {int_types}|{unsigned_int_types}|{signed_int_types}|{stdint_types_u}|{stdint_types_s}
float "float"|"double"|"long double"
float "long double"|"double"|"float"
string ["const "]"char"{whitespace*}"*"
pointer "*"
string "const "?"char"{whitespace}*"*"
%option noyywrap
%option nodefault
@ -38,16 +36,17 @@ pointer "*"
<COMMENT>\n { }
<COMMENT>"*"[^*\/\n]* { }
<INITIAL>"#"[^\n]"\n" { /* includes, macros, ... */ }
<INITIAL>"//"[^\n]\n { /* single line comment */ }
<INITIAL>^"#"[^\n]*"\n" { /* includes, macros, ... */ }
<INITIAL>"typedef" { return TYPEDEF; }
<INITIAL>"struct" { return STRUCT; }
<INITIAL>"struct" { return STRUCT; }
<INITIAL>{integer} { return LONG; }
<INITIAL>{int} { return LONG; }
<INITIAL>{float} { return DOUBLE; }
<INITIAL>{string} { return STRING; }
<INITIAL>{pointer} { return POINTER; }
<INITIAL>"*" { return POINTER; }
<INITIAL>";" { return SEMICOLON; }
<INITIAL>"{" { return OPEN_BRACES; }