https://github.com/rusuraluca/lftc/tree/main/lftc_lab9
lang.lxi
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
int currentLine = 1;
%}
%option noyywrap
identifier [a-zA-Z~][a-zA-Z0-9]*
integer_constant 0|[+|-]?[1-9][0-9]*
string_constant [\\"][a-zA-Z0-9 ]+[\\"]
char_constant [\\'][a-zA-Z0-9 ][\\']
%%
"int" {printf("Reserved word: %s\\n", yytext); return INT;}
"string" {printf("Reserved word: %s\\n", yytext); return STRING;}
"char" {printf("Reserved word: %s\\n", yytext); return CHAR;}
"in" {printf("Reserved word: %s\\n", yytext); return IN;}
"out" {printf("Reserved word: %s\\n", yytext); return OUT;}
"if" {printf("Reserved word: %s\\n", yytext); return IF;}
"else" {printf("Reserved word: %s\\n", yytext); return ELSE;}
"for" {printf("Reserved word: %s\\n", yytext); return FOR;}
"while" {printf("Reserved word: %s\\n", yytext); return WHILE;}
"return" {printf("Reserved word: %s\\n", yytext); return RETURN;}
"program" {printf("Reserved word: %s\\n", yytext); return PROGRAM;}
"list" {printf("Reserved word: %s\\n", yytext); return LIST;}
"finish" {printf("Reserved word: %s\\n", yytext); return FINISH;}
"+" {printf("Operator %s\\n", yytext); return plus;}
"+=" {printf("Operator %s\\n", yytext); return plusequal;}
"-" {printf("Operator %s\\n", yytext); return minus;}
"-=" {printf("Operator %s\\n", yytext); return minusequal;}
"*=" {printf("Operator %s\\n", yytext); return timesequal;}
"*" {printf("Operator %s\\n", yytext); return multiply;}
"/" {printf("Operator %s\\n", yytext); return divide;}
"/=" {printf("Operator %s\\n", yytext); return divideequal;}
"%" {printf("Operator %s\\n", yytext); return modulo;}
"<=" {printf("Operator %s\\n", yytext); return lessOrEqual;}
">=" {printf("Operator %s\\n", yytext); return moreOrEqual;}
"<" {printf("Operator %s\\n", yytext); return less;}
">" {printf("Operator %s\\n", yytext); return more;}
"==" {printf("Operator %s\\n", yytext); return equal;}
"!=" {printf("Operator %s\\n", yytext); return different;}
"=" {printf("Operator %s\\n", yytext); return assign;}
"&&" {printf("Operator %s\\n", yytext); return and;}
"||" {printf("Operator %s\\n", yytext); return or;}
"{" {printf("Separator %s\\n", yytext); return leftCurlyBracket;}
"}" {printf("Separator %s\\n", yytext); return rightCurlyBracket;}
"(" {printf("Separator %s\\n", yytext); return leftRoundBracket;}
")" {printf("Separator %s\\n", yytext); return rightRoundBracket;}
"[" {printf("Separator %s\\n", yytext); return leftBracket;}
"]" {printf("Separator %s\\n", yytext); return rightBracket;}
":" {printf("Separator %s\\n", yytext); return colon;}
";" {printf("Separator %s\\n", yytext); return semicolon;}
"," {printf("Separator %s\\n", yytext); return comma;}
"'" {printf("Separator %s\\n", yytext); return apostrophe;}
"\\"" {printf("Separator %s\\n", yytext); return quote;}
{identifier} {printf("Identifier: %s\\n", yytext); return identifier;}
{integer_constant} {printf("Number: %s\\n", yytext); return integer_constant;}
{string_constant} {printf("String: %s\\n", yytext); return string_constant;}
{char_constant} {printf("Character: %s\\n", yytext); return char_constant;}
[ \\t]+ {}
[\\n]+ {currentLine++;}
[0-9][a-zA-Z0-9_]* {printf("Illegal identifier at line %d\\n", currentLine);}
[+|-]0 {printf("Illegal numeric constant at line %d\\n", currentLine);}
[+|-]?[0][0-9]*([.][0-9]*)? {printf("Illegal numeric constant at line %d\\n", currentLine);}
[\\'][a-zA-Z0-9 ]{2,}[\\']|[\\'][a-zA-Z0-9 ][a-zA-Z0-9 ][\\'] {printf("Illegal character constant at line %d\\n", currentLine);}
[\\"][a-zA-Z0-9_]+|[a-zA-Z0-9_]+[\\"] {printf("Illegal string constant at line %d\\n", currentLine);}
%%
parser.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYDEBUG 1
int yylex(void);
void yyerror(char *s);
%}
%token PROGRAM
%token LIST
%token WHILE
%token INT
%token STRING
%token CHAR
%token FOR
%token VAR
%token IF
%token ELSE
%token OUT
%token IN
%token FINISH
%token RETURN
%token plus
%token minus
%token multiply
%token divide
%token modulo
%token lessOrEqual
%token moreOrEqual
%token less
%token more
%token equal
%token assign
%token different
%token isequal
%token not
%token plusequal
%token minusequal
%token divideequal
%token timesequal
%token or
%token and
%token leftCurlyBracket
%token rightCurlyBracket
%token leftRoundBracket
%token rightRoundBracket
%token leftBracket
%token rightBracket
%token semicolon
%token comma
%token colon
%token apostrophe
%token quote
%token identifier
%token integer_constant
%token char_constant
%token string_constant
%start program
%%
program : PROGRAM cmpdstmt
stmt : simplstmt semicolon | structstmt
cmpdstmt : leftCurlyBracket stmtlist rightCurlyBracket
stmtlist : stmt | stmt stmtlist
simplstmt : assignstmt | iostmt | declaration | arraydecl | returnstmt | expression
returnstmt: RETURN expression | RETURN
declaration : type identifier | type assignstmt
arraydecl : LIST type leftBracket integer_constant rightBracket identifier
assignstmt : identifier assign expression
expression : expression plus term | expression minusequal term | expression minus term | expression plusequal term | arrayaccess | term
arrayaccess : identifier leftBracket integer_constant rightBracket | identifier leftBracket identifier rightBracket
term : term multiply factor | term divide factor | term timesequal factor | term divideequal factor | term modulo factor | factor
factor : leftBracket expression rightBracket | identifier | constant
constant : integer_constant | string_constant | char_constant
iostmt : IN leftRoundBracket identifier rightRoundBracket | OUT leftRoundBracket identifier rightRoundBracket | OUT leftRoundBracket constant rightRoundBracket
type : INT | CHAR | STRING
structstmt : ifstmt | whilestmt
ifstmt : IF condition cmpdstmt | IF condition cmpdstmt ELSE cmpdstmt
whilestmt : WHILE condition cmpdstmt
condition : expression relation expression | negation | condition and condition | condition or condition
negation : not expression
relation : less | lessOrEqual | equal | more | moreOrEqual | isequal | different
%%
void yyerror(char *s) {
fprintf(stderr, "%s\\n", s);
}
extern FILE *yyin;
int main(int argc, char **argv)
{
if(argc>1) yyin : fopen(argv[1],"r");
if(argc>2 && !strcmp(argv[2],"-d")) yydebug = 1;
if(!yyparse()) fprintf(stderr, "\\tProgram is syntactically correct.\\n");
}