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){
- /*printf("identifier is '%s'\n", (*lp)->t.identifier);
- if ((*lp)->t.identifier){
- if((*lp)->t.identifier != variable){
- (*amountOfVariables)++;
- variable = (*lp)->t.identifier;
- }
- } */
- if (!acceptNumber(lp)){
- if (!acceptIdentifier(lp)){
- return 0;
- }
- }
- char *dummyVariable;
- dummyVariable = (char*)malloc(10 * sizeof(char));
- strcpy(dummyVariable, ((*lp)->t).identifier);
- acceptIdentifier(lp);
- if (acceptCharacter(lp, '^')){
- if ((*lp)->t.number != 1 && (*lp)->t.number != 0){
- if ((*lp)->t.number > *degree){
- *degree = (*lp)->t.number;
- }
- }if (!acceptNumber(lp)){
- return 0;
- }
- }
- 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 *variable = "";
- char *ar;
- List tl, tl1;
- printf("give an equation: ");
- ar = readInput();
- while (ar[0] != '!') {
- tl = tokenList(ar);
- //printf("the token list is ");
- printList(tl);
- tl1 = tl;
- if (acceptEquation(&tl1, °reeOfEquation, &amountOfVariables, variable) && tl1 == NULL) {
- printf("this is an equation");
- if(amountOfVariables <= 1){
- if (degreeOfEquation > 1 ){
- 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