-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
prabhakaran9397
committed
Mar 10, 2017
1 parent
cee00b7
commit c27bc7e
Showing
53 changed files
with
2,935 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
%{ | ||
|
||
#include <stdio.h> | ||
|
||
enum { | ||
NOTHING=0, | ||
DATATYPE, | ||
VARIABLE, | ||
TERMINATOR | ||
}; | ||
|
||
int seen = NOTHING; | ||
int check = 0; | ||
int line = 0; | ||
int offset = 0; | ||
|
||
%} | ||
|
||
datatype int|float|char | ||
variable [a-zA-Z_]*_[a-zA-Z0-9_]* | ||
terminator ; | ||
character . | ||
eol \n | ||
|
||
%% | ||
|
||
{datatype} { | ||
offset += yyleng; | ||
check = 1; | ||
seen = DATATYPE; | ||
} | ||
|
||
{variable} { | ||
if(seen == DATATYPE || seen == VARIABLE) { | ||
offset += yyleng; | ||
seen = VARIABLE; | ||
} | ||
else { | ||
seen = NOTHING; | ||
} | ||
} | ||
|
||
{terminator} { | ||
if(seen == VARIABLE) { | ||
offset += yyleng; | ||
seen = TERMINATOR; | ||
} | ||
else { | ||
seen = NOTHING; | ||
} | ||
} | ||
|
||
{eol} { | ||
++line; | ||
if(check && seen != TERMINATOR) { | ||
printf("Error at %d in line:[%d]\n", offset, line); | ||
seen = NOTHING; | ||
check = 0; | ||
} | ||
offset = 0; | ||
} | ||
|
||
{character} { | ||
++offset; | ||
} | ||
|
||
%% | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
yyin = fopen(argv[1], "r"); | ||
yylex(); | ||
fclose(yyin); | ||
return 0; | ||
} | ||
|
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
%option yylineno | ||
%option noyywrap | ||
|
||
type int|float|char | ||
id [a-zA-Z_][a-zA-Z_0-9]* | ||
ws [ \t]+ | ||
arr "["[0-9]+"]" | ||
|
||
%s WS | ||
%s ID | ||
%s ARR_COMMA_SEMI | ||
%s ERROR | ||
|
||
%{ | ||
int pos = 0; | ||
%} | ||
|
||
%% | ||
<INITIAL>{type} { BEGIN WS; pos+=yyleng; } | ||
<INITIAL>{ws} { pos+=yyleng; } | ||
<WS>{ws} { BEGIN ID; pos+=yyleng; } | ||
<ID>{id} { BEGIN ARR_COMMA_SEMI; pos+=yyleng; } | ||
<ARR_COMMA_SEMI>{arr} { pos+=yyleng; } | ||
<ARR_COMMA_SEMI>, { BEGIN ID; pos+=yyleng; } | ||
<ARR_COMMA_SEMI>; { BEGIN INITIAL; pos+=yyleng; } | ||
<ERROR>; { BEGIN INITIAL; pos+=yyleng; } | ||
<ERROR>. { pos+=yyleng; } | ||
\n { pos = 0; BEGIN INITIAL; } | ||
. { printf("Error:%d:%d\n", yylineno, pos+1); BEGIN ERROR; yyless(0); } | ||
%% | ||
|
||
int main(void) | ||
{ | ||
return yylex(); | ||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
%{ | ||
|
||
#include <stdio.h> | ||
|
||
enum { | ||
NOTHING=0, | ||
DATATYPE, | ||
VARIABLE, | ||
TERMINATOR | ||
}; | ||
|
||
int seen = NOTHING; | ||
int check = 0; | ||
int line = 0; | ||
int offset = 0; | ||
int function = 0; | ||
|
||
%} | ||
|
||
datatype int|float|char | ||
variable [a-zA-Z_][a-zA-Z0-9_]* | ||
subscript (\[[0-9]*\])+ | ||
terminator ; | ||
character . | ||
eol \n | ||
|
||
%% | ||
|
||
{datatype} { | ||
offset += yyleng; | ||
check = 1; | ||
seen = DATATYPE; | ||
} | ||
|
||
{variable} { | ||
if(seen == DATATYPE || seen == VARIABLE) { | ||
offset += yyleng; | ||
seen = VARIABLE; | ||
} | ||
else { | ||
seen = NOTHING; | ||
} | ||
} | ||
|
||
{terminator} { | ||
if(seen == VARIABLE) { | ||
offset += yyleng; | ||
seen = TERMINATOR; | ||
} | ||
else { | ||
seen = NOTHING; | ||
} | ||
} | ||
|
||
{eol} { | ||
++line; | ||
if(check && seen != TERMINATOR) { | ||
printf("Error at %d in line:[%d]\n", offset, line); | ||
seen = NOTHING; | ||
check = 0; | ||
} | ||
offset = 0; | ||
} | ||
|
||
{character} { | ||
++offset; | ||
} | ||
|
||
%% | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
yyin = fopen(argv[1], "r"); | ||
yylex(); | ||
fclose(yyin); | ||
return 0; | ||
} | ||
|
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
%{ | ||
#include "expr.yacc.h" | ||
%} | ||
|
||
WS [ \t] | ||
UOPT "++"|"--" | ||
BOPT "+"|"-"|"/"|"*"|"%" | ||
LOPT "&"|"|"|"^" | ||
ID [a-z]+ | ||
NUM [0-9]+ | ||
|
||
%% | ||
{BOPT}{WS}* { puts("(BOPT)"); return BOPT; } | ||
{UOPT}{WS}* { puts("(UOPT)"); return UOPT; } | ||
{LOPT}{WS}* { puts("(LOPT)"); return LOPT; } | ||
{ID}{WS}* { puts("(ID)"); return ID; } | ||
{NUM}{WS}* { puts("(NUM)"); return NUM; } | ||
\n { return *yytext; } | ||
. { return *yytext; } | ||
%% |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
%{ | ||
#include <bits/stdc++.h> | ||
extern int yylex(); | ||
extern int yyerror(const char*); | ||
%} | ||
|
||
%token UOPT BOPT LOPT ID NUM | ||
|
||
%% | ||
all: expr all | ||
| | ||
; | ||
|
||
expr: '\n' { puts("[nl]"); } | ||
| unary { puts("[**[unary]**]"); } | ||
| binary { puts("[**[binary]**]"); } | ||
| logical { puts("[**[logical]**]"); } | ||
| opand { puts("[opand]"); } | ||
; | ||
unary: UOPT opand { puts("[UOPT][opand]"); } | ||
| opand UOPT { puts("[opand][UOPT]"); } | ||
; | ||
binary: expr BOPT expr { puts("[expr][BOPT][expr]"); } | ||
; | ||
logical: expr LOPT expr { puts("[expr][LOPT][expr]"); } | ||
; | ||
opand: ID { puts("[ID]"); } | ||
| NUM { puts("[NUM]"); } | ||
; | ||
%% | ||
|
||
int yyerror(const char* s) | ||
{ | ||
fprintf(stderr, "%s\n", s); | ||
} | ||
|
||
int main(void) | ||
{ | ||
yyparse(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
int a, b; | ||
int c = a+++++b; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
if(a+b){a+c} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
%{ | ||
#include "ifelse.yacc.h" | ||
%} | ||
|
||
WS [ \t\n] | ||
UOPT "++"|"--" | ||
BOPT "+"|"-"|"/"|"*"|"%" | ||
LOPT "&"|"|"|"^" | ||
ID [a-z]+ | ||
NUM [0-9]+ | ||
|
||
%% | ||
if return IF; | ||
else return ELSE; | ||
{BOPT} return BOPT; | ||
{UOPT} return UOPT; | ||
{LOPT} return LOPT; | ||
{ID} return ID; | ||
{NUM} return NUM; | ||
{WS} ; | ||
. return *yytext; | ||
%% |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
%{ | ||
#include <bits/stdc++.h> | ||
extern int yylex(); | ||
extern int yyerror(const char*); | ||
%} | ||
|
||
%token UOPT BOPT LOPT ID NUM IF ELSE | ||
|
||
%% | ||
|
||
stmt : exprs { puts("[exprs]"); } | ||
| '{' stmt '}' { puts("[stmt]"); } | ||
| ifstmt { puts("[ifsmt]"); } | ||
; | ||
ifstmt : IF '(' expr ')' stmt { puts("[if]"); } | ||
| ifstmt ELSE stmt { puts("[ifelse]"); } | ||
; | ||
exprs : exprs expr ';' { puts("[exprs expr ;]"); } | ||
| | ||
; | ||
expr : unary { puts("[**[unary]**]"); } | ||
| binary { puts("[**[binary]**]"); } | ||
| logical { puts("[**[logical]**]"); } | ||
| opand { puts("[opand]"); } | ||
; | ||
unary : UOPT expr { puts("[UOPT][expr]"); } | ||
| expr UOPT { puts("[expr][UOPT]"); } | ||
; | ||
binary : expr BOPT expr { puts("[expr][BOPT][expr]"); } | ||
; | ||
logical : expr LOPT expr { puts("[expr][LOPT][expr]"); } | ||
; | ||
opand : ID { puts("[ID]"); } | ||
| NUM { puts("[NUM]"); } | ||
; | ||
%% | ||
|
||
int yyerror(const char* s) | ||
{ | ||
fprintf(stderr, "%s\n", s); | ||
} | ||
|
||
int main(void) | ||
{ | ||
yyparse(); | ||
} |
Binary file not shown.
Oops, something went wrong.