Created
December 20, 2013 22:05
-
-
Save semisight/8062414 to your computer and use it in GitHub Desktop.
Flex + Lemon problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%include { | |
#include <assert.h> | |
#include <stdlib.h> | |
} | |
%token_type { char* } | |
%syntax_error { fprintf(stderr, "Syntax error.\n"); } | |
%stack_overflow { fprintf(stderr, "Stack overflow.\n"); } | |
%parse_failure { fprintf(stderr, "Parse failed.\n"); } | |
%type top { int } | |
%type prog { int } | |
%type sexp_list { int } | |
%type sexp { int } | |
%type atom { int } | |
top(T) ::= prog(P). { T = P; printf("AST=%d.\n", T); } | |
prog(P) ::= sexp_list(L). { P = L; } | |
prog(P) ::= . { P = -1; } | |
sexp_list(L) ::= sexp(S) sexp_list(L1). { L = S + L1; } | |
sexp_list(L) ::= sexp(S). { L = S; } | |
sexp(S) ::= atom(A). { S = A; } | |
atom(A) ::= INT(V). { A = atoi(V); printf("found int of %s.\n", V); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%top{ | |
#include "grammar.h" | |
} | |
%option reentrant warn | |
%option noyywrap noinput nounput | |
%% | |
[0-9]+ { return INT; } | |
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "grammar.h" | |
#include "lexer.h" | |
// Declarations for the lemon parser. | |
void* ParseAlloc(void* (*allocProc)(size_t)); | |
void Parse(void*, int, char*); | |
void ParseFree(void*, void(*freeProc)(void*)); | |
int main() { | |
yyscan_t lexer; | |
void* parser = ParseAlloc(malloc); | |
int token; | |
yylex_init(&lexer); | |
yyset_in(stdin, lexer); | |
do { | |
token = yylex(lexer); | |
Parse(parser, token, yyget_text(lexer)); | |
} while(token != 0); | |
yylex_destroy(lexer); | |
ParseFree(parser, free); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment