Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %option noyywrap yylineno
- %{
- /* -*- mode: c -*- */
- #include <stdio.h>
- #include <string.h>
- #include "macro.h"
- #include "AST.h"
- #include "y.tab.h"
- extern YYSTYPE yylval;
- extern YYLTYPE yylloc;
- extern int debug;
- int charnum = 0;
- int linenum = 0;
- #define YY_USER_ACTION do { yylloc.first_line = yylloc.last_line = yylineno; yylloc.first_column = charnum; } while (0);
- %}
- ID [_a-z]+
- NUM [0-9]+
- INVALID {ID}{NUM}|{NUM}{ID}
- COMMENTL \[
- COMMENTR \]
- OPERATOR "+"|"-"|"*"|"/"|"%"
- COND "="|"!="|"<"|">"|"<="|">="
- CONDEXP IF|THEN|ELSE|ENDIF
- LOOP WLOOP|FLOOP
- WLOOP WHILE|DO|ENDWHILE
- FLOOP FOR|DOWN|FROM|TO|ENDFOR
- IO GET|PUT
- ASSIGN ":="
- DECLARE DECLARE
- IN IN
- END END
- SEMICOLON ";"
- PAR "("|")"
- KEYWORD {OPERATOR}|{COND}|{CONDEXP}|{LOOP}|{IO}|{ASSIGN}|{DECLARE}|{IN}|{END}|{SEMICOLON}|{PAR}|{INVALID}
- %x COMMENT
- %x ERROR
- %%
- {KEYWORD} {
- charnum += (strlen(yytext));
- debug("KEYWORD %s", yytext);
- //yylval.string = (char *)strdup(yytext);
- //return yytext;
- REJECT;
- }
- "+" { return PLUS; }
- "-" { return MINUS; }
- "*" { return TIMES; }
- "/" { return DIV; }
- "%" { return MOD; }
- "=" { return EQ; }
- "!=" { return DIFF; }
- "<" { return LE; }
- ">" { return GE; }
- "<=" { return LEQ; }
- ">=" { return GEQ; }
- IF { return IF; }
- THEN { return THEN; }
- ELSE { return ELSE; }
- ENDIF { return ENDIF; }
- WHILE { return WHILE; }
- DO { return DO; }
- ENDWHILE { return ENDWHILE; }
- FOR { return FOR; }
- DOWN { return DOWN; }
- FROM { return FROM; }
- TO { return TO; }
- DO { return DO; }
- ENDFOR { return ENDFOR; }
- GET { return GET; }
- PUT { return PUT; }
- := { return ASSIGN; }
- DECLARE { return DECLARE; }
- IN { return IN; }
- END { return END; }
- \( { return PARL; }
- \) { return PARR; }
- {SEMICOLON} { return SEMICOLON; }
- {INVALID} {
- error("%d:%d: unrecognized token \"%s\"", linenum, charnum, yytext);
- exit(1);
- }
- {ID} {
- debug("ID %s", yytext);
- charnum += (strlen(yytext));
- yylval.ast_node = new_stringnode('I', strdup(yytext)); // IDENTIFIER
- return PIDENTIFIER;
- }
- {NUM} {
- debug("INTEGER %s", yytext);
- charnum += (strlen(yytext));
- //yylval = atoi(yytext);
- yylval.ast_node = new_stringnode('N', strdup(yytext)); // NUM
- return NUM;
- }
- {COMMENTL} {
- BEGIN(COMMENT);
- charnum += (strlen(yytext));
- }
- <COMMENT>\n {
- /* eat multiline comments */
- charnum = 0;
- ++linenum;
- }
- <COMMENT>{COMMENTR} {
- BEGIN(INITIAL);
- charnum += (strlen(yytext));
- }
- <COMMENT>. {
- /* eat multiline comments */
- charnum += (strlen(yytext));
- }
- <COMMENT>{COMMENTL} {
- error("%d:%d: illegal nested comment",
- linenum, charnum);
- charnum += (strlen(yytext));
- exit(1);
- }
- <COMMENT><<EOF>> {
- error("%d:%d: unexpected EOF (in comment)",
- linenum, charnum);
- BEGIN(INITIAL);
- exit(1);
- }
- [ \t] {
- /* eat whitespaces */
- charnum += (strlen(yytext));
- }
- \n {
- /* eat whitespaces */
- charnum = 0;
- ++linenum;
- }
- . {
- BEGIN(ERROR);
- yymore();
- }
- <ERROR>[ \t] {
- error("%d:%d: unrecognized token \"%s\"",
- linenum, charnum, yytext);
- BEGIN(INITIAL);
- charnum += (strlen(yytext));
- exit(1);
- }
- <ERROR>\n {
- error("%d:%d: unrecognized token \"%s\"",
- linenum, charnum, yytext);
- BEGIN(INITIAL);
- charnum = 0;
- ++linenum;
- exit(1);
- }
- <ERROR><<EOF>> {
- error("%d:%d: unrecognized token \"%s\"",
- linenum, charnum);
- exit(1);
- }
- <ERROR>. {
- yymore();
- }
- %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement