added -lfl to LDFLAGS in main Makefile, added parser/parser.{lex,yy} and parser/Makefile (for real this time)
git-svn-id: svn://anubis/fart/trunk@79 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
31a7f0fedf
commit
1fe251f27d
2
Makefile
2
Makefile
@ -7,6 +7,8 @@ export CPPFLAGS += -I"$(shell pwd)"
|
||||
endif
|
||||
export CXXFLAGS := -Wall -O1
|
||||
|
||||
LDFLAGS := -lfl
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
.PHONY: $(TARGET)
|
||||
|
29
parser/Makefile
Normal file
29
parser/Makefile
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
FLEX := flex
|
||||
BISON := bison
|
||||
|
||||
PARSER := parser
|
||||
|
||||
COBJS := lex.yy.o
|
||||
CXXOBJS := $(PARSER).tab.o
|
||||
CXXOBJS += ASTNode.o
|
||||
|
||||
all: $(COBJS) $(CXXOBJS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c -o $@ $<
|
||||
|
||||
%.o: %.cc
|
||||
$(CXX) -c -o $@ $<
|
||||
|
||||
$(PARSER).tab.cc $(PARSER).tab.hh: $(PARSER).yy
|
||||
$(BISON) -d $<
|
||||
|
||||
lex.yy.o: lex.yy.c
|
||||
|
||||
lex.yy.c: $(PARSER).tab.hh
|
||||
lex.yy.c: $(PARSER).lex
|
||||
$(FLEX) $<
|
||||
|
||||
clean:
|
||||
-rm -f lex.yy.c $(PARSER).tab.cc $(PARSER).tab.hh *~ *.o
|
37
parser/parser.lex
Normal file
37
parser/parser.lex
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
%{
|
||||
#include "parser.tab.hh"
|
||||
%}
|
||||
|
||||
%%
|
||||
\+ return PLUS;
|
||||
- return MINUS;
|
||||
\* return STAR;
|
||||
\/ return DIVIDE;
|
||||
% return MOD;
|
||||
|
||||
; return SEMICOLON;
|
||||
: return COLON;
|
||||
\? return QUESTION;
|
||||
\$ return DOLLAR;
|
||||
\. return DOT;
|
||||
\" return DQUOTE;
|
||||
\' return SQUOTE;
|
||||
, return COMMA;
|
||||
|
||||
\{ return LCURLY;
|
||||
\} return RCURLY;
|
||||
\[ return LBRACKET;
|
||||
\] return RBRACKET;
|
||||
\( return LPAREN;
|
||||
\) return RPAREN;
|
||||
|
||||
0b[01]+ return BIN_NUMBER;
|
||||
0x[0-9A-Fa-f]+ return HEX_NUMBER;
|
||||
0[0-7]* return OCT_NUMBER;
|
||||
[1-9][0-9]* return DEC_NUMBER;
|
||||
|
||||
|
||||
\n /* ignore newlines */
|
||||
[ \t\v] /* ignore whitespace */
|
||||
%%
|
76
parser/parser.yy
Normal file
76
parser/parser.yy
Normal file
@ -0,0 +1,76 @@
|
||||
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
extern "C" {
|
||||
int yylex(void);
|
||||
}
|
||||
|
||||
extern FILE * yyin;
|
||||
|
||||
void yyerror(const char * str)
|
||||
{
|
||||
fprintf(stderr, "error: %s\n", str);
|
||||
}
|
||||
|
||||
int yywrap()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%token PLUS;
|
||||
%token MINUS;
|
||||
%token STAR;
|
||||
%token DIVIDE;
|
||||
%token MOD;
|
||||
|
||||
%token SEMICOLON;
|
||||
%token COLON;
|
||||
%token QUESTION;
|
||||
%token DOLLAR;
|
||||
%token DOT;
|
||||
%token DQUOTE;
|
||||
%token SQUOTE;
|
||||
%token COMMA;
|
||||
|
||||
%token LCURLY;
|
||||
%token RCURLY;
|
||||
%token LBRACKET;
|
||||
%token RBRACKET;
|
||||
%token LPAREN;
|
||||
%token RPAREN;
|
||||
|
||||
%token BIN_NUMBER;
|
||||
%token HEX_NUMBER;
|
||||
%token OCT_NUMBER;
|
||||
%token DEC_NUMBER;
|
||||
|
||||
%%
|
||||
|
||||
program: /* empty */
|
||||
| number program { printf("Saw a number\n"); }
|
||||
;
|
||||
|
||||
number: BIN_NUMBER
|
||||
| OCT_NUMBER
|
||||
| DEC_NUMBER
|
||||
| HEX_NUMBER
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
int parse(const char * fileName)
|
||||
{
|
||||
yyin = fopen(fileName, "r");
|
||||
if (yyin == NULL)
|
||||
{
|
||||
cerr << "Failed to open file '" << fileName << "'" << endl;
|
||||
return -1;
|
||||
}
|
||||
yyparse();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user