Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SyntaxAnalysis.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include "SyntaxAnalysisLib.h"
- #include "Matrix.h"
- #include "Lex.h"
- char* program;
- Token token;
- int brLinijePrveGreske = 0;
- int trenutniBrLinije=0;
- bool syntaxOK = true;
- void greska(){
- if (syntaxOK){
- brLinijePrveGreske = trenutniBrLinije;
- syntaxOK = false;
- printf("Prva greska na %d, liniji!", brLinijePrveGreske);
- exit(1);
- }
- }
- char* init()
- {
- FILE* f = fopen("program.txt", "rb");
- char* buffer;
- if (f != NULL)
- {
- long len;
- fseek(f, 0, SEEK_END);
- len = ftell(f);
- fseek(f, 0, SEEK_SET);
- buffer = (char*) malloc(len * sizeof(char));
- memset(buffer, 0x0, len);
- fread(buffer, sizeof(char), len, f);
- fclose(f);
- initFSM();
- return buffer;
- }
- else
- {
- return NULL;
- }
- }
- void cleanUp(char* buffer)
- {
- if (buffer != NULL)
- {
- free(buffer);
- }
- }
- void eat(TokenType t)
- {
- if (token.tokenType == t)
- {
- token = getNextTokenLex();
- }
- else
- {
- printSynError(token);
- }
- }
- void Q();
- void S();
- void L();
- void E();
- void Q()
- {
- if(token.tokenType == BEGIN)
- {
- trenutniBrLinije++;
- puts(token.value);
- eat(BEGIN);
- S();
- L();
- }
- else
- {
- printSynError(token);
- greska();
- }
- }
- void S()
- {
- if (token.tokenType == IF)
- {
- trenutniBrLinije++;
- puts(token.value);
- eat(IF);
- E();
- if (token.tokenType == THEN)
- {
- puts(token.value);
- eat(THEN);
- S();
- if(token.tokenType == ELSE)
- {
- puts(token.value);
- eat(ELSE);
- S();
- }
- else{
- printSynError(token);
- greska();
- }
- }
- else{
- printSynError(token);
- greska();
- }
- }
- else if (token.tokenType == PRINT)
- {
- puts(token.value);
- eat(PRINT);
- E();
- }
- else
- {
- printSynError(token);
- greska();
- }
- }
- void L()
- {
- if(token.tokenType == END)
- {
- trenutniBrLinije++;
- puts(token.value);
- eat(END);
- }
- else{
- S();
- L();
- }
- }
- void E()
- {
- if(token.tokenType == ID)
- {
- puts(token.value);
- eat(ID);
- if(token.tokenType == EQ){
- puts(token.value);
- eat(EQ);
- if(token.tokenType == NUM){
- puts(token.value);
- eat(NUM);
- }
- else{
- printSynError(token);
- greska();
- }
- }
- else{
- printSynError(token);
- greska();
- }
- }
- else{
- printSynError(token);
- greska();
- }
- }
- int main(int argc, char* argv[])
- {
- //
- program = init();
- token = getNextTokenLex();
- // Call function for the starting non-terminal symbol
- Q();
- if (syntaxOK){
- puts("SYNTAX OK");
- }
- // free resources
- cleanUp(program);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement