90 likes | 226 Views
This document outlines an LL(1) parser for a basic programming language that checks the syntax of programs defined within the structure of 'program', 'begin', and 'end' statements. It supports variable declarations and assignments using both float and int types. The parser uses a recursive descent approach, implementing functions to analyze variable lists, statements, and expressions. Syntax errors are printed to the console, helping users identify issues in their code. This project aims to provide a foundational understanding of syntax parsing in compiler design.
E N D
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c #include <stdio.h> #include "rpd.h" #include "symtab.h" int nextT; void checkfor(int token) { if (token != nextT) { printf("Syntax Error \n "); exit(0); } else nextT = yylex(); }
rpd.c void program() { checkfor(programT); checkfor('('); checkfor(identT); checkfor(')'); varlist(); checkfor(beginT); stlist(); checkfor(endT); printf("Compilering faerdig. Ingen fejl\n "); }
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c void type() { if (nextT == floatT) { checkfor(floatT); Ctype= floatT; } else { if (nextT == intT) { checkfor(intT); Ctype= intT; } else { printf("Syntax Error 'int' eller 'float' type forvendtet! \n "); exit(0); } } }
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c void identelmlist() { if (nextT == ',') { checkfor(','); checkfor(identT); identelmlist(); } }
rpd.c int main() { init_sym(); nextT = yylex(); program(); }
LL(1)-Parser statement-> identT '=' expA ';' expA-> term expBexpB-> termopr term expB | etermopr-> '+' | '-'term-> factor termBtermB-> factoropr factor termB | efactoropr-> '*' | '/'factor-> uopr expA | '(' expA ')' | realT | identTuopr-> '-' | e