Last active
July 28, 2023 13:32
-
-
Save bahadirbklg/03053e05dcc8aed33c787348c4beae98 to your computer and use it in GitHub Desktop.
compiler design homework - parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ctype.h> | |
char currentToken; | |
void getToken() { | |
currentToken = getchar(); | |
} | |
int P(); | |
int C(); | |
int I(); | |
int W(); | |
int A(); | |
int Ç(); | |
int G(); | |
int E(); | |
int T(); | |
int U(); | |
int F(); | |
int K(); | |
int R(); | |
int main() { | |
// Set the initial value of n to 0 | |
int n = 0; | |
// Read the input string | |
printf("Enter the input string: "); | |
getToken(); | |
// Call the P() function to parse and verify the input | |
if (P()) { | |
printf("Input is valid.\n"); | |
} else { | |
printf("Input is invalid.\n"); | |
} | |
return 0; | |
} | |
// P → {C} '.' | |
int P() { | |
while (C()) {} | |
if (currentToken == '.') { | |
getToken(); | |
return 1; | |
} | |
return 0; | |
} | |
// C → I | W | A | Ç | G | |
int C() { | |
if (I() || W() || A() || Ç() || G()) { | |
return 1; | |
} | |
return 0; | |
} | |
// I → '[' E '?' C{ C } ':' C{ C } ']' | '[' E '?' C{C} ']' | |
int I() { | |
if (currentToken == '[') { | |
getToken(); | |
if (E() && currentToken == '?') { | |
getToken(); | |
while (C()) {} | |
if (currentToken == ':') { | |
getToken(); | |
while (C()) {} | |
} | |
if (currentToken == ']') { | |
getToken(); | |
return 1; | |
} | |
} | |
} | |
return 0; | |
} | |
// W → '{' E '?' C{C} '}' | |
int W() { | |
if (currentToken == '{') { | |
getToken(); | |
if (E() && currentToken == '?') { | |
getToken(); | |
while (C()) {} | |
if (currentToken == '}') { | |
getToken(); | |
return 1; | |
} | |
} | |
} | |
return 0; | |
} | |
// A → K '=' E ';' | |
int A() { | |
if (K() && currentToken == '=') { | |
getToken(); | |
if (E() && currentToken == ';') { | |
getToken(); | |
return 1; | |
} | |
} | |
return 0; | |
} | |
// Ç → '<' E ';' | |
int Ç() { | |
if (currentToken == '<') { | |
getToken(); | |
if (E() && currentToken == ';') { | |
getToken(); | |
return 1; | |
} | |
} | |
return 0; | |
} | |
// G → '>' K ';' | |
int G() { | |
if (currentToken == '>') { | |
getToken(); | |
if (K() && currentToken == ';') { | |
getToken(); | |
return 1; | |
} | |
} | |
return 0; | |
} | |
// E → T {('+' | '-') T} | |
int E() { | |
if (T()) { | |
while (currentToken == '+' || currentToken == '-') { | |
getToken(); | |
if (!T()) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
return 0; | |
} | |
// T → U {('*' | '/' | '%') U} | |
int T() { | |
if (U()) { | |
while (currentToken == '*' || currentToken == '/' || currentToken == '%') { | |
getToken(); | |
if (!U()) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
return 0; | |
} | |
// U → F '^' U | F | |
int U() { | |
if (F()) { | |
if (currentToken == '^') { | |
getToken(); | |
if (!U()) { | |
return 0; | |
} | |
} | |
return 1; | |
} | |
return 0; | |
} | |
// F → '(' E ')' | K | R | |
int F() { | |
if (currentToken == '(') { | |
getToken(); | |
if (E() && currentToken == ')') { | |
getToken(); | |
return 1; | |
} | |
} else if (K() || R()) { | |
return 1; | |
} | |
return 0; | |
} | |
// K → 'a' | 'b' | ... | 'z' | |
int K() { | |
if (islower(currentToken)) { | |
getToken(); | |
return 1; | |
} | |
return 0; | |
} | |
// R → '0' | '1' | ... | '9' | |
int R() { | |
if (isdigit(currentToken)) { | |
getToken(); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment