Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 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. *
  7. * <equation> ::= <expression> '=' <expression>
  8. *
  9. * <expression> ::= <term> { '+' <term> | '-' <term> }
  10. *
  11. *
  12. * <term> ::= <number> | <number> <identifier> | <number> <identifier> '^' <number>
  13. *
  14. * Input for the recognizer is the token list constructed by the scanner (in scanner.c).
  15. * For the recognition of a token list the method of *recursive descent* is used.
  16. * It relies on the use of three functions for the recognition and processing of
  17. * terms, factors and expressions, respectively.
  18. * These three functions are defined with mutual recursion, corresponding with the
  19. * structure of the BNF grammar.
  20. */
  21.  
  22. #include <stdio.h> /* getchar, printf */
  23. #include <stdlib.h> /* NULL */
  24. #include "scanner.h"
  25. #include "recognizeExp.h"
  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) {
  67. if(acceptNumber(lp)){
  68. if(acceptIdentifier(lp)){
  69. if(acceptCharacter(lp, '^')){
  70. if(acceptNumber(lp)){
  71. return 1;
  72. }
  73. }
  74. }else{
  75. return 0;
  76. }
  77. }
  78.  
  79. int acceptExpression(List *lp) {
  80. if ( !acceptTerm(lp) ) {
  81. return 0;
  82. }
  83. while ( acceptCharacter(lp,'+') || acceptCharacter(lp,'-') ) {
  84. if ( !acceptTerm(lp) ) {
  85. return 0;
  86. }
  87. } /* no + or -, so we reached the end of the expression */
  88. return 1;
  89. }
  90.  
  91. int acceptEquation(List *lp) {
  92. if(!acceptExpression(lp)){
  93. return 0;
  94. }
  95. while(acceptExpression(lp) && acceptCharacter(lp, '=') && acceptExpression(lp)){
  96.  
  97. }
  98. }
  99.  
  100. /* The next function can be used to demonstrate the recognizer.
  101. */
  102.  
  103. void recognizeExpressions() {
  104. char *ar;
  105. List tl, tl1;
  106. printf("give an expression: ");
  107. ar = readInput();
  108. while (ar[0] != '!') {
  109. tl = tokenList(ar);
  110. printf("the token list is ");
  111. printList(tl);
  112. tl1 = tl;
  113. if ( acceptExpression(&tl1) && tl1 == NULL ) {
  114. printf("this is an expression\n");
  115. } else {
  116. printf("this is not an expression\n");
  117. }
  118. free(ar);
  119. freeTokenList(tl);
  120. printf("\ngive an expression: ");
  121. ar = readInput();
  122. }
  123. free(ar);
  124. printf("good bye\n");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement