Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* recognizeExp.c, Gerard Renardel, 29 January 2014
- *
- * In this file a recognizer acceptExpression is definined that can recognize
- * arithmetical expressions generated by the following BNF grammar:
- *
- * <expression> ::= <term> { '+' <term> | '-' <term> }
- *
- * <term> ::= <factor> { '*' <factor> | '/' <factor> }
- *
- * <factor> ::= <number> | <identifier> | '(' <expression> ')'
- *
- * Input for the recognizer is the token list constructed by the scanner (in scanner.c).
- * For the recognition of a token list the method of *recursive descent* is used.
- * It relies on the use of three functions for the recognition and processing of
- * terms, factors and expressions, respectively.
- * These three functions are defined with mutual recursion, corresponding with the
- * structure of the BNF grammar.
- */
- #include <stdio.h> /* getchar, printf */
- #include <stdlib.h> /* NULL */
- #include <string.h>
- #include "scanner.h"
- #include "recognizeExp.h"
- /* The functions acceptNumber, acceptIdentifier and acceptCharacter have as
- * (first) argument a pointer to an token list; moreover acceptCharacter has as
- * second argument a character. They check whether the first token
- * in the list is a number, an identifier or the given character, respectively.
- * When that is the case, they yield the value 1 and the pointer points to the rest of
- * the token list. Otherwise they yield 0 and the pointer remains unchanged.
- */
- int acceptNumber(List *lp) {
- if (*lp != NULL && (*lp)->tt == Number) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- int acceptIdentifier(List *lp) {
- if (*lp != NULL && (*lp)->tt == Identifier) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- int acceptCharacter(List *lp, char c) {
- if (*lp != NULL && (*lp)->tt == Symbol && ((*lp)->t).symbol == c) {
- *lp = (*lp)->next;
- return 1;
- }
- return 0;
- }
- /* The functions acceptFactor, acceptTerm and acceptExpression have as
- * argument a pointer to a token list. They check whether the token list
- * has an initial segment that can be recognized as factor, term or expression, respectively.
- * When that is the case, they yield the value 1 and the pointer points to the rest of
- * the token list. Otherwise they yield 0 and the pointer remains unchanged.
- */
- int acceptTerm(List *lp, int *degree, int *amountOfVariables, char *variable){
- int power = 0;
- int tempVar2 = acceptNumber(lp);
- List stored = *lp;
- int tempVar = acceptIdentifier(lp);
- if(tempVar2 == 0 && tempVar == 0){
- return 0;
- }
- if(tempVar == 1){
- char *dummyVariable = (*stored).t.identifier;
- if(strcmp(dummyVariable, variable)){
- (*amountOfVariables)++;
- strcpy(variable, dummyVariable);
- }
- }
- if (acceptCharacter(lp, '^')){
- //if no variable was in front of ^
- if (!tempVar){
- return 0;
- }
- power = (*lp)->t.number;
- // if the power is not a number
- if (!acceptNumber(lp)){
- *degree = -1;
- return 0;
- //or if power was smaller than 0 (probably redundant since acceptNumber should only be for naturals)
- }else if(power < 0){
- return 0;
- }
- if (power > *degree){
- *degree = power;
- }
- }else if(tempVar){
- *degree = 1;
- }
- return 1;
- }
- int acceptExpression(List *lp, int *degree, int *amountOfVariables, char *variable) {
- if (!acceptTerm(lp, degree, amountOfVariables, variable) && !((acceptCharacter(lp, '-') && acceptTerm(lp, degree, amountOfVariables, variable)))) {
- return 0;
- }
- while (acceptCharacter(lp, '+') || acceptCharacter(lp, '-')) {
- if (!acceptTerm(lp, degree, amountOfVariables, variable)) {
- return 0;
- }
- } /* no + or -, so we reached the end of the expression */
- return 1;
- }
- int acceptEquation(List *lp, int *degree, int *amountOfVariables, char *variable){
- if (acceptExpression(lp, degree, amountOfVariables, variable) && acceptCharacter(lp, '=') && acceptExpression(lp, degree, amountOfVariables, variable)){
- return 1;
- }
- return 0;
- }
- /* The function recognizeEquation demonstrates the recognizer. */
- void recognizeEquation() {
- int degreeOfEquation = 0;
- int amountOfVariables = 0;
- char *ar;
- List tl, tl1;
- printf("give an equation: ");
- ar = readInput();
- while (ar[0] != '!') {
- degreeOfEquation = 0;
- amountOfVariables = 0;
- char variable[10000] = "*";
- tl = tokenList(ar);
- printList(tl);
- tl1 = tl;
- if (acceptEquation(&tl1, °reeOfEquation, &amountOfVariables, variable) && tl1 == NULL && degreeOfEquation >= 0) {
- printf("this is an equation");
- if(amountOfVariables == 1 && degreeOfEquation >= 0){
- printf(" in 1 variable of degree %d\n", degreeOfEquation);
- }else{
- printf(", but not in 1 variable\n");
- }
- } else {
- printf("this is not an equation\n");
- }
- free(ar);
- freeTokenList(tl);
- printf("\ngive an equation: ");
- ar = readInput();
- }
- free(ar);
- printf("good bye\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement