Advertisement
BlankOD

Untitled

Feb 21st, 2022
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 0 0
  1. /* recognizeExp.c, Gerard Renardel, 29 January 2014
  2.  *
  3.  * In this file a recognizer acceptExpression is definined that can recognize
  4.  * arithmetical expressions generated by the following BNF grammar:
  5.  *
  6.  * <expression>  ::= <term> { '+'  <term> | '-' <term> }
  7.  *
  8.  * <term>       ::= <factor> { '*' <factor> | '/' <factor> }
  9.  *
  10.  * <factor>     ::= <number> | <identifier> | '(' <expression> ')'
  11.  *
  12.  * Input for the recognizer is the token list constructed by the scanner (in scanner.c).
  13.  * For the recognition of a token list the method of *recursive descent* is used.
  14.  * It relies on the use of three functions for the recognition and processing of
  15.  * terms, factors and expressions, respectively.
  16.  * These three functions are defined with mutual recursion, corresponding with the
  17.  * structure of the BNF grammar.
  18.  */
  19.  
  20. #include <stdio.h>  /* getchar, printf */
  21. #include <stdlib.h> /* NULL */
  22. #include <string.h>
  23. #include "scanner.h"
  24. #include "recognizeExp.h"
  25.  
  26.  
  27. /* The functions acceptNumber, acceptIdentifier and acceptCharacter have as
  28.  * (first) argument a pointer to an token list; moreover acceptCharacter has as
  29.  * second argument a character. They check whether the first token
  30.  * in the list is a number, an identifier or the given character, respectively.
  31.  * When that is the case, they yield the value 1 and the pointer points to the rest of
  32.  * the token list. Otherwise they yield 0 and the pointer remains unchanged.
  33.  */
  34.  
  35. int acceptNumber(List *lp) {
  36.   if (*lp != NULL && (*lp)->tt == Number) {
  37.     *lp = (*lp)->next;
  38.     return 1;
  39.   }
  40.   return 0;
  41. }
  42.  
  43. int acceptIdentifier(List *lp) {
  44.   if (*lp != NULL && (*lp)->tt == Identifier) {
  45.     *lp = (*lp)->next;
  46.     return 1;
  47.   }
  48.   return 0;
  49. }
  50.  
  51. int acceptCharacter(List *lp, char c) {
  52.   if (*lp != NULL && (*lp)->tt == Symbol && ((*lp)->t).symbol == c) {
  53.     *lp = (*lp)->next;
  54.     return 1;
  55.   }
  56.   return 0;
  57. }
  58.  
  59. /* The functions acceptFactor, acceptTerm and acceptExpression have as
  60.  * argument a pointer to a token list. They check whether the token list
  61.  * has an initial segment that can be recognized as factor, term or expression, respectively.
  62.  * When that is the case, they yield the value 1 and the pointer points to the rest of
  63.  * the token list. Otherwise they yield 0 and the pointer remains unchanged.
  64.  */
  65.  
  66. int acceptTerm(List *lp, int *degree, int *amountOfVariables, char *variable){
  67.   int power = 0;
  68.   int tempVar2 = acceptNumber(lp);
  69.   List stored = *lp;
  70.   int tempVar = acceptIdentifier(lp);
  71.   if(tempVar2 == 0 && tempVar == 0){
  72.     return 0;
  73.   }
  74.   if(tempVar == 1){
  75.     char *dummyVariable = (*stored).t.identifier;
  76.     if(strcmp(dummyVariable, variable)){
  77.       (*amountOfVariables)++;
  78.       strcpy(variable, dummyVariable);
  79.     }
  80.   }
  81.  
  82.  
  83.   if (acceptCharacter(lp, '^')){
  84.     //if no variable was in front of ^
  85.     if (!tempVar){
  86.       return 0;
  87.     }
  88.     power = (*lp)->t.number;
  89.     // if the power is not a number
  90.     if (!acceptNumber(lp)){
  91.       *degree = -1;
  92.       return 0;
  93.     //or if power was smaller than 0 (probably redundant since acceptNumber should only be for naturals)
  94.     }else if(power < 0){
  95.       return 0;
  96.     }
  97.  
  98.     if (power > *degree){
  99.       *degree = power;
  100.     }
  101.   }else if(tempVar){
  102.     *degree = 1;
  103.   }
  104.   return 1;
  105. }
  106.  
  107. int acceptExpression(List *lp, int *degree, int *amountOfVariables, char *variable) {
  108.   if (!acceptTerm(lp, degree, amountOfVariables, variable) && !((acceptCharacter(lp, '-') && acceptTerm(lp, degree, amountOfVariables, variable)))) {
  109.     return 0;
  110.   }
  111.   while (acceptCharacter(lp, '+') || acceptCharacter(lp, '-')) {
  112.     if (!acceptTerm(lp, degree, amountOfVariables, variable)) {
  113.       return 0;
  114.     }
  115.   } /* no + or -, so we reached the end of the expression */
  116.   return 1;
  117. }
  118.  
  119. int acceptEquation(List *lp, int *degree, int *amountOfVariables, char *variable){
  120.   if (acceptExpression(lp, degree, amountOfVariables, variable) && acceptCharacter(lp, '=') && acceptExpression(lp, degree, amountOfVariables, variable)){
  121.     return 1;
  122.   }
  123.   return 0;
  124. }
  125.  
  126. /* The function recognizeEquation demonstrates the recognizer. */
  127. void recognizeEquation() {
  128.   int degreeOfEquation = 0;
  129.   int amountOfVariables = 0;
  130.   char *ar;
  131.   List tl, tl1;
  132.   printf("give an equation: ");
  133.   ar = readInput();
  134.   while (ar[0] != '!') {
  135.     degreeOfEquation = 0;
  136.     amountOfVariables = 0;
  137.     char variable[10000] = "*";
  138.     tl = tokenList(ar);
  139.     printList(tl);
  140.     tl1 = tl;
  141.     if (acceptEquation(&tl1, &degreeOfEquation, &amountOfVariables, variable) && tl1 == NULL && degreeOfEquation >= 0) {
  142.       printf("this is an equation");
  143.       if(amountOfVariables == 1 && degreeOfEquation >= 0){
  144.         printf(" in 1 variable of degree %d\n", degreeOfEquation);
  145.       }else{
  146.         printf(", but not in 1 variable\n");
  147.       }
  148.  
  149.     } else {
  150.       printf("this is not an equation\n");
  151.     }
  152.     free(ar);
  153.     freeTokenList(tl);
  154.     printf("\ngive an equation: ");
  155.     ar = readInput();
  156.   }
  157.   free(ar);
  158.   printf("good bye\n");
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement