Question

Compiler Design Please help me with this lexical analysis In C Using FInite automata and a...

Compiler Design

Please help me with this lexical analysis In C
Using FInite automata and a driver.
With file test scanner , main, scanner, token.h

  • All case sensitive
  • Each scanner error should display "Scanner Error:" followed by details including the line number ff available
  • Alphabet
    • all English letters (upper and lower), digits, plus the extra characters as seen below, plus WS
    • No other characters allowed and they should generate errors
  • Identifiers
    • begin with a lower case letter and
    • continue with any number of letters or underscores
    • you may assume no identifier is longer than 8 characters
  • Keywords (reserved, suggested individual tokens)
  • Operators and delimiters group.
  • Integers
    • any sequence of decimal digits, no sign, no decimal point
    • you may assume no number longer than 8 characters
  • Comments start with $ and end with $
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Program to implement lexical analyser in C.

Store the inputs in a file "input.txt"in the same location

where this program is saved.

*/

#include<stdio.h>

#include<string.h>

#include<ctype.h>

int isKeyword(char buffer[]) //function to check if the string is keyword

{

char keywords[32][10] = {"auto","break","case","char","const","continue","default",

"do","double","else","enum","extern","float","for","goto",

"if","int","long","register","return","short","signed",

"sizeof","static","struct","switch","typedef","union",

"unsigned","void","volatile","while"};

int i, flag = 0;

for(i = 0; i < 32; ++i)

{

if(strcmp(keywords[i], buffer) == 0)

{

flag = 1; //if string matches with any keyword

break;

}

}

return flag;

}

void main() //main function

{

char ch, buffer[15], operators[] = "+-*/%=";

FILE *fp; //fp is the file pointer used to open file

int i,j=0;

clrscr();

fp = fopen("inputfile.txt","r"); //opening input file

if(fp == NULL) //no such file exists

{

printf("\n\t Error while opening the file");

exit(0);

}

while((ch = fgetc(fp)) != EOF)

{

for(i = 0; i < 6; ++i)

{

if(ch == operators[i])

printf("\n\t %c is operator",ch);

}

if(isalnum(ch))

{

buffer[j++] = ch;

}

else if((ch == ' ' || ch == '\n') && (j != 0))

{

buffer[j] = '\0';

j = 0;

if(isKeyword(buffer) == 1)

printf("\n\t %s is Keyword",buffer);

else

printf("\n\t %s is Indentifier",buffer);

}

}

fclose(fp); //closing the input file

getch();

}

Add a comment
Know the answer?
Add Answer to:
Compiler Design Please help me with this lexical analysis In C Using FInite automata and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

    Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase of program compilation Assignment: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a Java, program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described...

  • The first project involves modifying the attached lexical analyzer and the compilation listing ge...

    The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make the following modifications to the lexical analyzer, scanner.l: 1. A second type of comment should be added that begins with // and ends with the end of line. As with the existing comment, no token should be returned. 2. The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should...

  • If i could get any guidance on how to get this started it will be great....

    If i could get any guidance on how to get this started it will be great. My prof. literally just gave us the information below (which are literally just instructions) and I am unsure on how to get started. For this assignment we are going to create a function that reads an input stream and classifies it into “tokens” from a language that we define here. In this language we make the following rules: ● An identifier is a letter...

  • Tokeniser in C++

    TokenisersBackgroundThe primary task of any language translator is to work out how the structure and meaning of an input in a given language so that an appropriate translation can be output in another language. If you think of this in terms of a natural language such as English. When you attempt to read a sentence you do not spend your time worrying about what characters there are, how much space is between the letters or where lines are broken. What...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment...

    Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • okay so here is my c++ code and the errors im really stuck on fixing what...

    okay so here is my c++ code and the errors im really stuck on fixing what i did wrong it seems to be the same repeated error our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses A your program should use a loop and your morse code printing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT