Untitled

 avatar
user_2381398
plain_text
a year ago
4.3 kB
1
Indexable
Never
%{
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdio>
#include <unistd.h>
#include <string.h>
#include "parser.tab.h"
#define CONCAT     strcat(buf,yytext)
#define t_token(t) {CONCAT; printf("<%s>\n",#t);}
#define tokenInteger(t,i) {CONCAT; printf("<%s:%d>\n",t,i);}
#define tokenBoolean	{CONCAT; printf("<%s:%s>\n","boolean",yytext);}
#define tokenString(t,s) {CONCAT; printf("<%s:%s>\n",t,s);}
#define tokenFloat(t,f) {CONCAT; printf("<%s:%f>\n",t,f);}
#define MAX_LINE_LENG 256

int lineNm = 1;
char buf[MAX_LINE_LENG];
char strBuffer[MAX_LINE_LENG];
bool Opt_L = true;
bool prtT = false;

%}

digit [0-9]
integer {digit}+
letter [a-zA-Z]
identifier {letter}({digit}|{letter})*
real ({digit}+\.{digit}*)
stringConstants (\"(\"\"|[^"\n])*\")
whitespace [ \t]+


/* states */
%x COMMENT
%x STRING

%%
"." {t_token('.');return DOT;}
"," {t_token(',');return COMMA;}
":=" {t_token(':=');return COLONEQUALS;}
":" {t_token(':');return COLON;}
";" {t_token(';');return SEMICOLON;}
"(" {t_token('(');return LP;}
")" {t_token(')');return RP;}
"[" {t_token('[');return LSB;}
"]" {t_token(']');return RSB;}
"{" {t_token('{');return LCB;}
"}" {t_token('}');return RCB;}

-[0-9]+   {
    yylval.integerValue = atoi(yytext);
    return INTEGERVAL;
}


"+" {t_token('+');return PLUS;}
"-" {t_token('-');return MINUS;}
"*" {t_token('*');return MULTIPLY;}
"/" {t_token('/');return DIVIDE;}
"mod" {t_token('mod');return MOD;}

"<" {t_token('<');return LT;}
"<=" {t_token('<=');return LE;}
">" {t_token('>');return GT;}
"=>" {t_token('>=');return EG;}
"==" {t_token('==');return EQ;}
"=" {t_token('=');return EQUALS;}
"not=" {t_token('not=');return NOTEQUALS;}

"and" {t_token('and');return AND;}
"or" {t_token('or');return OR;}
"not" {t_token('not');return NOT;}

"array" {t_token(ARRAY);return ARRAY;}

"bool" {t_token(BOOL);return BOOL;}
"char" {t_token(CHAR);return CHAR;}
"const" {t_token(CONST);return CONST;}
"decreasing" {t_token(DECREASING);return DECREASING;}
"default" {t_token(DEFAULT);return DEFAULT;}
"to" {t_token(TO);return TO;}
"do" {t_token(DO);return DO;}
"else" {t_token(ELSE);return ELSE;}
"begin" {t_token(BEGIN);return BEGIN_KEYWORD;}
"end" {t_token(END);return END;}
"exit" {t_token(EXIT);return EXIT;}
"for" {t_token(FOR);return FOR;}
"function" {t_token(FUNCTION);return FUNCTION;}
"get" {t_token(GET);return GET;}
"if" {t_token(IF);return IF;}
"int" {t_token(INT);return INT;}
"loop" {t_token(LOOP);return LOOP;}
"of" {t_token(OF);return OF;}
"put" {t_token(PUT);return PUT;}
"procedure" {t_token(PROCEDURE);return PROCEDURE;}
"real" {t_token(REAL);return REAL;}
"result" {t_token(RESULT);return RESULT;}
"return" {t_token(RETURN);return RETURN;}
"skip" {t_token(SKIP);return SKIP;}
"string" {t_token(STRING);return STRING;}
"then" {t_token(THEN);return THEN;}
"var" {t_token(VAR);return VAR;}
"when" {t_token(WHEN);return WHEN;}
"false" {tokenBoolean;yylval.integerValue = 0;return FALSEVAL;}
"true" {tokenBoolean;yylval.integerValue = 1;return TRUEVAL;}
{integer}".."{integer} {
    return INT_RANGE;
}

{real} {
	double real_val = atof(yytext);
	tokenFloat("real", real_val);
	yylval.floatValue=real_val;
	return FLOATVAL;
}
{identifier} {
	tokenString("id",yytext);
	yylval.idString = new string(yytext);
	return IDENTIFIER;
}

{integer} {
	tokenInteger("integer",atoi(yytext));
	yylval.integerValue = atoi(yytext);
	return INTEGERVAL;
}



\"[^\"\n]*\" { 
    strncpy(yylval.strValue, yytext+1, strlen(yytext)-2); 
    yylval.strValue[strlen(yytext)-2] = '\0'; 
    return STRINGVAL; 
}


<STRING>. {
    CONCAT;
    strcat(strBuffer,yytext);
}

"%"[^\n]* {CONCAT;}
"{%" {
	CONCAT;
	BEGIN COMMENT;
}

<COMMENT>\n {
	CONCAT;
	printf("%d: %s", lineNm, buf);
	lineNm++;
	buf[0] = '\0';
}

<COMMENT>. {
	CONCAT;
}

<COMMENT>"%}" {
	CONCAT;
	BEGIN INITIAL;
}

{whitespace} {CONCAT;}

\n {
	CONCAT;
	if(Opt_L)
	printf("%d: %s", lineNm, buf);
	lineNm++;
	buf[0] = '\0';
}


. {
	CONCAT;	
	printf("%d:%s\n", lineNm, buf);
	lineNm++;
	printf("bad character:'%s'\n",yytext);
	exit(-1);
}
%%

int yywrap(void)
{
    puts("-----the file is end");
    return 1;
}