daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Sep 18th, 2018 92 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /* front.c - a lexical analyzer system for simple
  2.              arithmetic expressions */
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.   /* Global declarations */
  6.   /* Variables */
  7. int charClass;
  8. char lexeme[100];
  9. char nextChar;
  10. int lexLen;
  11. int token;
  12. int nextToken;
  13. FILE *in_fp, *fopen();
  14. /* Function declarations */
  15. void addChar();
  16. void getChar();
  17. void getNonBlank();
  18. int lex();
  19. /* Character classes */
  20.  
  21. #define LETTER 0
  22. #define DIGIT 1
  23. #define UNKNOWN 99
  24. /* Token codes */
  25.  
  26. #define INT_LIT 10
  27. #define IDENT 11
  28. #define ASSIGN_OP 20
  29. #define ADD_OP 21
  30. #define SUB_OP 22
  31. #define MULT_OP 23
  32. #define DIV_OP 24
  33. #define LEFT_PAREN 25
  34. #define RIGHT_PAREN 26
  35. /******************************************************/
  36. /* main driver */
  37.  
  38. int main() {
  39.   /* Open the input data file and process its contents */
  40.   if ((in_fp = fopen("front.in", "r")) == NULL)
  41.     printf("ERROR - cannot open front.in \n");
  42.   else {
  43.     getChar();
  44.     do {
  45.       lex();
  46.     }
  47.     while (nextToken != EOF);
  48.   }
  49. }
  50. /*****************************************************/
  51. /* lookup - a function to lookup operators and parentheses
  52.             and return the token */
  53. int lookup(char ch) {
  54.   switch (ch) {
  55.       case '(':
  56.         addChar();
  57.         nextToken = LEFT_PAREN;
  58.         break;
  59.       case ')':
  60.         addChar();
  61.         nextToken = RIGHT_PAREN;
  62.         break;
  63.       case '+':
  64.         addChar();
  65.         nextToken = ADD_OP;
  66.         break;
  67.       case '-':
  68.         addChar();
  69.         nextToken = SUB_OP;
  70.         break;
  71.       case '*':
  72.         addChar();
  73.         nextToken = MULT_OP;
  74.         break;
  75.  
  76.       case '/':
  77.         addChar();
  78.         nextToken = DIV_OP;
  79.         break;
  80.       default:
  81.         addChar();
  82.         nextToken = EOF;
  83.         break;
  84.   }
  85.   return nextToken;
  86. }
  87. /*****************************************************/
  88. /* addChar - a function to add nextChar to lexeme */
  89. void addChar() {
  90.   if (lexLen <= 98) {
  91.     lexeme[lexLen++] = nextChar;
  92.     lexeme[lexLen] = 0;
  93.   } else
  94.     printf("Error - lexeme is too long \n");
  95. }
  96.  
  97. /*****************************************************/
  98. /* getChar - a function to get the next character of
  99.              input and determine its character class */
  100. void getChar() {
  101.   if ((nextChar = getc(in_fp)) != EOF) {
  102.     if (isalpha(nextChar)) { charClass = LETTER; }
  103.     else if (isdigit(nextChar)) { charClass = DIGIT; }
  104.     else { charClass = UNKNOWN; }
  105.  
  106.   } else { charClass = EOF; }
  107. }
  108. /*****************************************************/
  109. /* getNonBlank - a function to call getChar until it
  110.                  returns a non-whitespace character */
  111. void getNonBlank() {
  112.   while (isspace(nextChar)) { getChar(); }
  113. }
  114.  
  115. /*****************************************************/
  116. /* lex - a simple lexical analyzer for arithmetic
  117.        expressions */
  118. int lex() {
  119.   lexLen = 0;
  120.   getNonBlank();
  121.   switch (charClass) {
  122.     /* Parse identifiers */
  123.   case
  124.   LETTER:
  125.     addChar();
  126.     getChar();
  127.     while (charClass == LETTER || charClass == DIGIT) {
  128.       addChar();
  129.       getChar();
  130.     }
  131.     nextToken = IDENT;
  132.     break;
  133.     /* Parse integer literals */
  134.   case
  135.   DIGIT:
  136.     addChar();
  137.     getChar();
  138.     while (charClass == DIGIT) {
  139.       addChar();
  140.       getChar();
  141.     }
  142.     nextToken = INT_LIT;
  143.     break;
  144.     /* Parentheses and operators */
  145.   case
  146.   UNKNOWN:
  147.     lookup(nextChar);
  148.     getChar();
  149.     break;
  150.     /* EOF */
  151.   case
  152.   EOF:
  153.     nextToken = EOF;
  154.     lexeme[0] = 'E';
  155.     lexeme[1] = 'O';
  156.     lexeme[2] = 'F';
  157.     lexeme[3] = 0;
  158.     break;
  159.   } /* End of switch */
  160.   printf("Next token is: %d, Next lexeme is %s\n",
  161.     nextToken, lexeme);
  162.   return nextToken;
  163. } /* End of function lex */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top