61 lines
2.1 KiB
Plaintext
61 lines
2.1 KiB
Plaintext
#include <stdio.h>
|
|
#include <iostream>
|
|
using namespace std;
|
|
int yylex(void); // defini dans progL.cpp, utilise par yyparse()
|
|
void yyerror(const char * msg); // defini plus loin, utilise par yyparse()
|
|
int lineNumber; // notre compteur de lignes
|
|
extern FILE * yyin; // defini dans progL.cpp, utilise par main()
|
|
|
|
%}
|
|
|
|
%token START END // les lexemes que doit fournir yylex()
|
|
%token ASSIGN SEMICOLON
|
|
%token IDENT REAL INTEGER
|
|
%start program // l'axiome de notre grammaire
|
|
|
|
%%
|
|
|
|
program : stmtList
|
|
;
|
|
stmtList : stmtList stmt { CompoundStatement* stmt = $1; stmt.setLastStatement($2); $$ = stmt; }
|
|
| stmt { CompoundStatement* stmt = new CompoundStatement(); stmt.setLastStatement($1); $$ = stmt; }
|
|
;
|
|
stmt : assign
|
|
| ifStmt
|
|
| WHILE expr EOL stmtList EOL WHILEEND {}
|
|
| DO EOL stmtList EOL LPWHILE expr {}
|
|
| FOR assignStmt TO expr EOL stmtList EOL NEXT { $$ = new ForStatement($2, $4, $6);}
|
|
| FOR assignStmt TO expr STEP expr EOL stmtList EOL NEXT { $$ = new ForStatement($2, $4, $8, $6);}
|
|
| GOTO IDENT
|
|
| LABEL IDENT
|
|
| BREAK
|
|
| RETURN
|
|
| STOP
|
|
| expr
|
|
;
|
|
expr : IDENT { cerr << "integer expr" << endl; }
|
|
| NUMBER { cerr << "real expr" << endl; }
|
|
| expr OPERATOR expr { cerr << "ident expr" << endl; }
|
|
;
|
|
assignStmt : expr ASSIGN IDENT
|
|
;
|
|
ifStmt : IF expr EOL THEN EOL stmtList EOL IFEND { $$ = new IfStatement($2, $6); }
|
|
| IF expr EOL THEN EOL stmtList EOL ELSE EOL stmtList EOL IFEND { $$ = new IfStatement($2, $6, $10); }
|
|
| expr => assign { $$ = new IfStatement($2, $3); }
|
|
|
|
%%
|
|
|
|
void yyerror(const char * msg)
|
|
{
|
|
cerr << "line " << lineNumber << ": " << msg << endl;
|
|
}
|
|
|
|
int main(int argc,char ** argv)
|
|
{
|
|
if(argc>1)
|
|
yyin=fopen(argv[1],"r"); // check result !!!
|
|
lineNumber=1;
|
|
if(!yyparse())
|
|
cerr << "Success" << endl;
|
|
return(0);
|
|
} |