Advertisement
BlankOD

Untitled

Feb 18th, 2022
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 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.   /*printf("identifier is '%s'\n", (*lp)->t.identifier);
  68.   if ((*lp)->t.identifier){
  69.     if((*lp)->t.identifier != variable){
  70.       (*amountOfVariables)++;
  71.       variable = (*lp)->t.identifier;
  72.     }
  73.   } */
  74.  
  75.   if (!acceptNumber(lp)){
  76.     if (!acceptIdentifier(lp)){
  77.       return 0;
  78.     }
  79.   }
  80.   char *dummyVariable;
  81.   dummyVariable = (char*)malloc(10 * sizeof(char));
  82.  
  83.   strcpy(dummyVariable, ((*lp)->t).identifier);
  84.   acceptIdentifier(lp);
  85.  
  86.   if (acceptCharacter(lp, '^')){
  87.     if ((*lp)->t.number != 1 && (*lp)->t.number != 0){
  88.       if ((*lp)->t.number > *degree){
  89.         *degree = (*lp)->t.number;
  90.       }
  91.     }if (!acceptNumber(lp)){
  92.       return 0;
  93.     }
  94.   }
  95.  
  96.   return 1;
  97. }
  98. int acceptExpression(List *lp, int *degree, int *amountOfVariables, char *variable) {
  99.   if (!acceptTerm(lp, degree, amountOfVariables, variable) && !((acceptCharacter(lp, '-') && acceptTerm(lp, degree, amountOfVariables, variable)))) {
  100.     return 0;
  101.   }
  102.   while (acceptCharacter(lp, '+') || acceptCharacter(lp, '-')) {
  103.     if (!acceptTerm(lp, degree, amountOfVariables, variable)) {
  104.       return 0;
  105.     }
  106.   } /* no + or -, so we reached the end of the expression */
  107.   return 1;
  108. }
  109.  
  110. int acceptEquation(List *lp, int *degree, int *amountOfVariables, char*variable){
  111.   if (acceptExpression(lp, degree, amountOfVariables, variable) && acceptCharacter(lp, '=') && acceptExpression(lp, degree, amountOfVariables, variable)){
  112.     return 1;
  113.   }
  114.   return 0;
  115. }
  116.  
  117. /* The function recognizeEquation demonstrates the recognizer. */
  118. void recognizeEquation() {
  119.   int degreeOfEquation = 0;
  120.   int amountOfVariables = 0;
  121.   char *variable = "";
  122.   char *ar;
  123.   List tl, tl1;
  124.   printf("give an equation: ");
  125.   ar = readInput();
  126.   while (ar[0] != '!') {
  127.     tl = tokenList(ar);
  128.     //printf("the token list is ");
  129.     printList(tl);
  130.     tl1 = tl;
  131.     if (acceptEquation(&tl1, &degreeOfEquation, &amountOfVariables, variable) && tl1 == NULL) {
  132.       printf("this is an equation");
  133.       if(amountOfVariables <= 1){
  134.         if (degreeOfEquation > 1 ){
  135.           printf(" in 1 variable of degree %d\n", degreeOfEquation);
  136.         }
  137.       } else{
  138.         printf(", but not in 1 variable\n");
  139.       }
  140.  
  141.     } else {
  142.       printf("this is not an equation\n");
  143.     }
  144.     free(ar);
  145.     freeTokenList(tl);
  146.     printf("\ngive an equation: ");
  147.     ar = readInput();
  148.   }
  149.   free(ar);
  150.   printf("good bye\n");
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement