Skip to content

Commit

Permalink
lexer and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglundong committed Nov 6, 2012
0 parents commit 7a97be5
Show file tree
Hide file tree
Showing 7 changed files with 854 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#Compiler Construction

##Author:
Jinglun Dong

##Dependency:
Lexer using flex
Parser using bison

##How to compile:
$flex lexer.l
$bison -d parser.y
$gcc lex.yy.c parser.tab.c -o parser.out

##How to run:
$./parser.out < inputStream
OR:
$./parser.out inputFile.txt

##Output:
stdout for rules and symbo table goes to rule.out and symtable.out
stderr for rules and symbo table goes to rule.err and symtable.err
16 changes: 16 additions & 0 deletions README.md~
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Lexer using flex
Parser using bison

How to compile:
$flex lexer.l
$bison -d parser.y
$gcc lex.yy.c parser.tab.c -o parser.out

How to run:
$./parser.out < inputStream
OR:
$./parser.out inputFile.txt

Output:
stdout for rules and symbo table goes to rule.out and symtable.out
stderr for rules and symbo table goes to rule.err and symtable.err
126 changes: 126 additions & 0 deletions lexer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*Assignment 1. Jinglun DONG*/
%{
#include "parser.tab.h"
#include "parser.h"

/*Symbol Table*/
struct token entity[1000];

int count = 0; //number of tokens
%}

ID {letter}({letter}|{digit}|_)*

letter [A-Za-z]

digit [0-9]

INT {digit}+

STR \"[^\n"]+\"
WS [ \t\r\n]+
%%
and {return(AND);}
begin {return(_BEGIN);}
forward {return(FORWARD);}
div {return(DIV);}
do {return(DO);}
else {return(ELSE);}
end {return(END);}
for {return(FOR);}
function {return(FUNCTION);}
if {return(IF);}
array {return(ARRAY);}
mod {return(MOD);}
not {return(NOT);}
of {return(OF);}
or {return(OR);}
procedure {return(PROCEDURE);}
program {return(PROGRAM);}
record {return(RECORD);}
then {return(THEN);}
to {return(TO);}
type {return(TYPE);}
var {return(VAR);}
while {return(WHILE);}
sym {return(SYM);}
{ID} {yylval.num = (int)installID(); return(ID);}
{INT} {yylval.num = (int)installNum(); return(INT);}
{STR} {yylval.num = (int)installStr(); return(STR);}
"." {yylval.num = DOT; return(DOT);}
"," {yylval.num = COMMA; return(COMMA);}
":" {yylval.num = COLON; return(COLON);}
";" {yylval.num = SEMIC; return(SEMIC);}
":=" {yylval.num = ASSIGN; return(ASSIGN);}
".." {yylval.num = DOTDOT; return(DOTDOT);}
"(" {yylval.num = LPAR; return(LPAR);}
")" {yylval.num = RPAR; return(RPAR);}
"[" {yylval.num = LBRAC; return(LBRAC);}
"]" {yylval.num = RBRAC; return(RBRAC);}
"=" {yylval.num = EQUAL; return(EQUAL);}
"<" {yylval.num = LESS; return(LESS);}
"<=" {yylval.num = LESSEQUAL; return(LESSEQUAL);}
">" {yylval.num = GREATER; return(GREATER);}
">=" {yylval.num = GREATEREQUAL; return(GREATEREQUAL);}
"<>" {yylval.num = NOTEQUAL; return(NOTEQUAL);}
"+" {yylval.num = PLUS; return(PLUS);}
"-" {yylval.num = MINUS; return(MINUS);}
"*" {yylval.num = STAR; return(STAR);}
{WS} {/* skip blanks and tabs */}
"{"[^{}]*"}" {/* ignore comments */}
{digit}{ID} {return(UNKOWN);}
. {return(UNKOWN);}
%%
int yywrap(void){return 1;}
int installID(){
char* str = strdup(yytext);
int i;
for(i=0;i<count;i++){
if(!strcmp(entity[i].value,str)){
return i;
}
}
int index = count;
entity[index].value = str;
entity[index].property = "ID";
count++;
return index;
}
int installNum(){
return 0;
}
int installStr(){
return 0;
}
/*
int main( int argc, char **argv ){
++argv, --argc;
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
int tokenNum;
while(tokenNum = yylex()){
printf("%d\n",tokenNum);
}
}
*/
21 changes: 21 additions & 0 deletions parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef PARSER_H
#define PARSER_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

struct token{
char* property;
char* value;
char* type;
};

struct ids {
int index;
struct ids* next;
int depth;
};

struct ids* newid(int id, struct ids* next);
#endif
21 changes: 21 additions & 0 deletions parser.h~
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef HEADER_H
#define HEADER_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

struct token{
char* property;
char* value;
char* type;
};

struct ids {
int index;
struct ids* next;
int depth;
};

struct ids* newid(int id, struct ids* next);
#endif
Loading

0 comments on commit 7a97be5

Please sign in to comment.