mirror of
https://github.com/RustyRaptor/compilers.git
synced 2026-12-03 15:15:17 -07:00
added grammar rules still needs debugging
This commit is contained in:
1 parent
43800a3c50
commit
befab27f4a
4 files changed
+134
-23
No files matched your search
@@ -0,0 +1,47 @@
|
||||
// This test should have errors.
|
||||
// comment them out until you have no errors
|
||||
|
||||
extern func print_int(int) void;
|
||||
extern func print_string(string) void;
|
||||
|
||||
package foo {
|
||||
|
||||
var x int;
|
||||
var A[100] bool;
|
||||
|
||||
func foo(x int) void { { var x int; }
|
||||
while ( x <= y )
|
||||
{
|
||||
|
||||
x = 1 + 2 - 3 * 4 / 6 % 7 && 8 || ! f(); // operators
|
||||
return(3);
|
||||
return(b); // where b was declared as "var b bool;"
|
||||
return();
|
||||
return;
|
||||
break;
|
||||
continue;
|
||||
|
||||
if (a < b)
|
||||
{ a = a << b >> c;}
|
||||
|
||||
if (a < b)
|
||||
{ a = a << b >> c;
|
||||
c = "hello "; } // string
|
||||
else
|
||||
{ print_string("hello"); }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func main() int { foo(1);
|
||||
for ( i=1,j=2; i <= k; i=i+1, j=j+2) // for loop
|
||||
{
|
||||
var x,y,z int;
|
||||
var x,y,z bool;
|
||||
i = print_int(i);
|
||||
x = 0xBad0FF; // HEX
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // end of package foo
|
||||
@@ -0,0 +1,42 @@
|
||||
// This test should run without error
|
||||
|
||||
extern func print_int(int) void;
|
||||
extern func print_string(string) void;
|
||||
|
||||
package foo {
|
||||
|
||||
var x int;
|
||||
var y int = 100;
|
||||
var A[100] bool;
|
||||
|
||||
func foo(x int) void {
|
||||
var x int;
|
||||
var x int;
|
||||
var y bool;
|
||||
while ( x <= y )
|
||||
{
|
||||
|
||||
x = 1 + 2 - 3 * 4 / 6 % 7 && 8 || ! f(); // operators
|
||||
return(3);
|
||||
return(b); // where b was declared as "var b bool;"
|
||||
return();
|
||||
return;
|
||||
break;
|
||||
|
||||
if (a < b)
|
||||
{ a = a << b >> c;}
|
||||
|
||||
if (a < b)
|
||||
{ a = a << b >> c;
|
||||
c = "hello "; } // string
|
||||
else
|
||||
{ print_string("hello"); }
|
||||
|
||||
} // of while
|
||||
} // of foo
|
||||
|
||||
func main() int { foo(1);
|
||||
} // of main
|
||||
|
||||
|
||||
} // end of package foo
|
||||
+32
-16
@@ -1,17 +1,18 @@
|
||||
/* Small LEX routine which returns two formal tokens (INTEGER and VARIABLE)
|
||||
along with single string elements like '+'.
|
||||
|
||||
This LEX definition is the companion to the docalc.y YACC routine which
|
||||
is a simple calculator
|
||||
/* Small LEX routine which returns two formal tokens (INTEGER and VARIABLE)
|
||||
along with single string elements like '+'.
|
||||
|
||||
Shaun Cooper
|
||||
January 2015
|
||||
*/
|
||||
This LEX definition is the companion to the docalc.y YACC routine which
|
||||
is a simple calculator
|
||||
|
||||
Shaun Cooper
|
||||
January 2015
|
||||
*/
|
||||
|
||||
%{
|
||||
int mydebug=0;
|
||||
|
||||
#include "y.tab.h"
|
||||
int yylex();
|
||||
int mydebug=1;
|
||||
|
||||
%}
|
||||
|
||||
@@ -20,9 +21,15 @@ CHANGES:
|
||||
- added the DECAF tokens
|
||||
*/
|
||||
|
||||
|
||||
%%
|
||||
"&&" {return(T_AND);}
|
||||
"=" {return(T_ASSIGN);}
|
||||
|
||||
"=" {
|
||||
if (mydebug) fprintf(stderr,"T_ASSIGN found\n");
|
||||
return(T_ASSIGN);
|
||||
}
|
||||
|
||||
"bool" {return(T_BOOLTYPE);}
|
||||
"break" {return(T_BREAK);}
|
||||
"continue" {return(T_CONTINUE);}
|
||||
@@ -36,7 +43,12 @@ CHANGES:
|
||||
">=" {return(T_GEQ);}
|
||||
">" {return(T_GT);}
|
||||
"if" {return(T_IF);}
|
||||
"int" {return(T_INTTYPE);}
|
||||
|
||||
"int" {
|
||||
if (mydebug) fprintf(stderr,"T_INTTYPE found\n");
|
||||
return(T_INTTYPE);
|
||||
|
||||
}
|
||||
"<<" {return(T_LEFTSHIFT);}
|
||||
"<=" {return(T_LEQ);}
|
||||
"!=" {return(T_NEQ);}
|
||||
@@ -48,7 +60,7 @@ CHANGES:
|
||||
"string" {return(T_STRINGTYPE);}
|
||||
"true" {return(T_TRUE);}
|
||||
"var" {return(T_VAR);}
|
||||
"void" {return(T_VOID);}
|
||||
"void" {if (mydebug) fprintf(stderr,"T_VOID found\n");return(T_VOID);}
|
||||
"while" {return(T_WHILE);}
|
||||
|
||||
\'.\' {
|
||||
@@ -63,7 +75,7 @@ CHANGES:
|
||||
|
||||
[a-zA-Z][a-zA-Z0-9_]* {
|
||||
if (mydebug) {
|
||||
fprintf(stderr,"Letter found\n");
|
||||
fprintf(stderr,"Letter found %s\n", yytext);
|
||||
}
|
||||
yylval.string=strdup(yytext);
|
||||
return(T_ID);
|
||||
@@ -78,17 +90,21 @@ CHANGES:
|
||||
if (mydebug) fprintf(stderr,"Whitespace found\n");
|
||||
}
|
||||
|
||||
[=\-+*/%();{},\[\]<>!] {
|
||||
[\-+*/%();{},\[\]<>!] {
|
||||
if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
|
||||
return (*yytext);
|
||||
}
|
||||
|
||||
\n {
|
||||
if (mydebug) fprintf(stderr,"carriage return %c\n",*yytext);
|
||||
return (*yytext);
|
||||
}
|
||||
\/\/.* { /* this is a comment line for double slash Return nothing, keep going */ }
|
||||
%%
|
||||
|
||||
int yywrap(void)
|
||||
{ return 1;}
|
||||
{ return 1;}
|
||||
|
||||
/* void yyerror(const char *str)
|
||||
{
|
||||
fprintf(stderr,"Error | Line: %d\n%s\n",yylineno,str);
|
||||
} */
|
||||
+13
-7
@@ -3,11 +3,12 @@
|
||||
#include <ctype.h>
|
||||
int yylex();
|
||||
|
||||
|
||||
extern int yylineno;
|
||||
void yyerror (s) /* Called by yyparse on error */
|
||||
char *s;
|
||||
{
|
||||
printf ("%s \n", s);
|
||||
printf ("FUCK SHIT %d \n", yylineno);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,11 +59,14 @@ from LEX and how the operators are associated */
|
||||
%token T_VOID
|
||||
%token T_WHILE
|
||||
|
||||
|
||||
|
||||
%% /* end specs, begin rules */
|
||||
Program : Externs T_PACKAGE T_ID '{' FieldDecls MethodDecls '}'
|
||||
;
|
||||
|
||||
Externs : ExternDefn
|
||||
Externs : /* empty */
|
||||
| ExternDefn Externs
|
||||
;
|
||||
|
||||
ExternDefn : T_EXTERN T_FUNC T_ID '(' ExternParmList ')' MethodType ';'
|
||||
@@ -86,7 +90,7 @@ FieldDecl : T_VAR T_ID Type ';'
|
||||
FieldDecl : T_VAR T_ID ArrayType ';'
|
||||
;
|
||||
|
||||
FieldDecl : T_VAR T_ID Type '=' Constant ';'
|
||||
FieldDecl : T_VAR T_ID Type T_ASSIGN Constant ';'
|
||||
;
|
||||
|
||||
MethodDecls : /* empty */
|
||||
@@ -101,7 +105,7 @@ IdTypeList : /* empty */
|
||||
;
|
||||
|
||||
IdTypeList1 : T_ID Type
|
||||
| T_ID Type ',' IdTypeList1
|
||||
| T_ID Type ',' IdTypeList
|
||||
;
|
||||
|
||||
Block : '{' VarDecls Statements '}'
|
||||
@@ -127,7 +131,7 @@ Statement : Block
|
||||
Statement : Assign ';'
|
||||
;
|
||||
|
||||
Assign : Lvalue '=' Expr
|
||||
Assign : Lvalue T_ASSIGN Expr
|
||||
;
|
||||
|
||||
Lvalue : T_ID
|
||||
@@ -224,6 +228,8 @@ Constant : T_INTCONSTANT
|
||||
;
|
||||
%% /* end of rules, start of program */
|
||||
|
||||
int main(){
|
||||
int main(){
|
||||
|
||||
yyparse();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user