Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- // #include "pl0yacc.tab.h"
- #include "pl0yacc.tab.hpp"
- void yyerror(char*);
- //#define YY_USER_INIT yylloc.first_line = 1;
- /*handle locations */
- int yylcolumn = 1;
- /*The macro YY_USER_INIT is used to define the action to be
- performed before the first scan*/
- #define YY_USER_INIT yylloc.first_line = yylloc.first_column = 1;
- /*The macro YY_USER_ACTION is executed always before the execution
- of the action whenever token is recognized by lex*/
- // #define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno;
- // yylloc.first_column = yylcolumn;
- // yylloc.last_column = yylcolumn+yyleng-1;
- // yylcolumn += yyleng;
- %}
- number [0-9]+
- identifier [a-zA-Z][a-zA-Z|0-9]*
- ws [ \t]+
- newline [\r\n]+
- %%
- ":=" {
- //printf("ASSIGNMENT");
- //columnCount += 2;
- //yylval.num = columnCount;
- return ASSIGNMENT;
- }
- "/" {
- //printf("DIVIDE");
- //++columnCount;
- //yylval.num = columnCount;
- return DIVIDE;
- }
- "*" {
- //printf("MULTIPLY");
- //++columnCount;
- //yylval.num = columnCount;
- return MULTIPLY;
- }
- "-" {
- //printf("SUB");
- //++columnCount;
- //yylval.num = columnCount;
- return SUB;
- }
- "+" {
- ///printf("ADD");
- //++columnCount;
- //yylval.num = columnCount;
- return ADD;
- }
- ")" {
- //printf("RB");
- //++columnCount;
- //yylval.num = columnCount;
- return RB;
- }
- "(" {
- //printf("LB");
- //++columnCount;
- //yylval.num = columnCount;
- return LB;
- }
- "<>" {
- //printf("NE");
- //columnCount += 2;
- //yylval.num = columnCount;
- return NE;
- }
- ">=" {
- //printf("GE");
- //columnCount += 2;
- //yylval.num = columnCount;
- return GE;
- }
- "<=" {
- //printf("LE");
- //columnCount += 2;
- //yylval.num = columnCount;
- return LE;
- }
- "=" {
- //printf("EQ");
- //++columnCount;
- ///yylval.num = columnCount;
- return EQ;
- }
- "<" {
- //printf("LT");
- //++columnCount;
- //yylval.num = columnCount;
- return LT;
- }
- ">" {
- //printf("GT");
- ///++columnCount;
- //yylval.num = columnCount;
- return GT;
- }
- "," {
- //printf("COMMA");
- //++columnCount;
- //yylval.num = columnCount;
- return COMMA;
- }
- "." {
- //printf("DOT");
- //++columnCount;
- //yylval.num = columnCount;
- return DOT;
- }
- ";" {
- //printf("SEMICOLON");
- //++columnCount;
- //yylval.num = columnCount;
- return SEMICOLON;
- }
- begin|BEGIN {
- //printf("BEGIN");
- //columnCount += 5;
- //yylval.num = columnCount;
- return BEGN;
- }
- call|CALL {
- //printf("CALL");
- //columnCount += 4;
- //yylval.num = columnCount;
- return CALL;
- }
- const|CONST {
- //printf("CONST");
- //columnCount += 5;
- //yylval.num = columnCount;
- return CONST;
- }
- do|DO {
- //printf("DO");
- //columnCount += 2;
- //yylval.num = columnCount;
- return DO;
- }
- end|END {
- //printf("END");
- //columnCount += 3;
- //yylval.num = columnCount;
- return END;
- }
- if|IF {
- //printf("IF");
- //columnCount += 2;
- //yylval.num = columnCount;
- return IF;
- }
- odd|ODD {
- //printf("ODD");
- //columnCount += 3;
- //yylval.num = columnCount;
- return ODD;
- }
- procedure|PROCEDURE {
- //printf("PROCEDURE");
- //columnCount += 9;
- //yylval.num = columnCount;
- return PROCEDURE;
- }
- then|THEN {
- //printf("THEN");
- //columnCount += 4;
- //yylval.num = columnCount;
- return THEN;
- }
- var|VAR {
- //printf("VAR");
- //columnCount += 3;
- //yylval.num = columnCount;
- return VAR;
- }
- while|WHILE {
- //printf("WHILE");
- //columnCount += 5;
- //yylval.num = columnCount;
- return WHILE;
- }
- {number} {
- //int length = strlen(yytext);
- //columnCount += length;
- //yylval.num = atoi(yytext);
- return NUMBER;
- //printf("NUMBER");
- }
- {identifier} {
- yylval.str = strdup(yytext);
- return ID;
- //printf("IDENIFIER");
- }
- {ws}$ {
- //printf("WHITE_SPACES_AT_END")
- }
- {ws} {
- //printf("WHITE_SPACES");
- }
- {newline} {
- //yylloc.first_line++;
- //printf("NL");
- }
- %%
- int yywrap(void) {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement