Question

For part one of this assignment, write a program (parse.c) that contains a function to parse...

For part one of this assignment, write a program (parse.c) that contains a function to parse a single line of input and and prints out the individual tokens.

Part 1 - Single Line Parser

For part one of this assignment, you will need to learn how to do some parsing (or more accurately--lexing a line, you do not have to check for correctness). You have previously done some parsing with the cycle count tool perhaps using functions such as strcmp. The little parser we are building now would be similar to the first part of building a compiler or interpeter for a programming language. For our next assignment, we are going to need to use our parser to parse commands users enter into their terminal (i.e. shell). A shell like we have been using can be thought of as an interactive interpreter of a command-line language performing the following:

  1. It takes in a line of code (READ)
  2. Parses the command (EVALUATES)
  3. Then executes the command and its arguments (PRINT output)
  4. Loops and then prints out any output. (LOOP)

We call this a REPL interpreter

For the first part of this assignment, we are going to break a string into individual tokens (or lexemes).

  • The first token will be the command
  • The second through N number of tokens (i.e. lexemes) will be any number of arguments.
  • (Optionally) The N+1 tokens through M tokens will be another command with arguments.

For those who like a more formal definition, a grammar describing how to break each string into individual lexemes looks like the following:

command [args]* [ | commmand [args]* ]*

  • command is what you type in (e.g. 'ls')
  • args are any number of arguments (e.g. -l)
  • And there may be a pipe ('|') sending our output elsewhere.
    • The pipe ('|') command means that we are taking the previous commands output as using that as input into our next command.
  • The star(asterisk) means that any number of arguments(zero to as infinite) could follow (See Kleene star).

For part one of this assignment, write a program (parse.c) that contains a function to parse a single line of input and and prints out the individual tokens. You may re-use as much as parse.c as you like for implementing your shell for future assignments.

Example Input and Output

For example (See the line of input and the associated tokens separated onto a new line):

  • ./parse "cd ."

    • cd
      .
      
  • ./parse "ls -l"

    • ls
      -l
      
  • ./parse "cat compile | head 2"

    • cat
      compile.py
      |
      head
      2
      

    Note: Your program will read in from the command-line the arguments to parse (e.g. ./parse "cat compile | head 2")

    Hint: Wrapping the command in quotes makes the first argument a string-- and you need to parse a single string.

Additional Hints for Part 1

  • The function strtok may be useful for splitting up a line of input.
    • strtok works by splitting up an input based on a delimeter that indicates a new token is starting(in our example a space).
    • strtok is part of the string.h library, so be sure to include it.
  • You may use other strategies outside of strtok however--there are many ways to parse your input.
  • My expectation is Part 1 of this assignment should be relatively short and you may reused the functions for implementing the mini-shell in the next assignment!

parse.c

// Implement a working parser in this file that splits text into individual tokens.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

contents of the file parse.c

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Checking for Delimiters.
bool isDelim(char ch)
{
   if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
       ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
       ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
       ch == '[' || ch == ']' || ch == '{' || ch == '}')
       return (true);
   return (false);
}

// Checking for Operators
bool isOp(char ch)
{
   if (ch == '+' || ch == '-' || ch == '*' ||
       ch == '/' || ch == '>' || ch == '<' ||
       ch == '=')
       return (true);
   return (false);
}

// Checking Valid Identifiers.
bool isValidId(char* str) // here the function name is 'isValidId'
{
   if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
       str[0] == '3' || str[0] == '4' || str[0] == '5' ||
       str[0] == '6' || str[0] == '7' || str[0] == '8' ||
       str[0] == '9' || isDelim(str[0]) == true)
       return (false);
   return (true);
}

// Checking for keywords.
bool isKey(char* str)
{
   if (!strcmp(str, "if") || !strcmp(str, "else") ||
       !strcmp(str, "while") || !strcmp(str, "do") ||
       !strcmp(str, "break") ||
       !strcmp(str, "continue") || !strcmp(str, "int")
       || !strcmp(str, "double") || !strcmp(str, "float")
       || !strcmp(str, "return") || !strcmp(str, "char")
       || !strcmp(str, "case") || !strcmp(str, "char")
       || !strcmp(str, "sizeof") || !strcmp(str, "long")
       || !strcmp(str, "short") || !strcmp(str, "typedef")
       || !strcmp(str, "switch") || !strcmp(str, "unsigned")
       || !strcmp(str, "void") || !strcmp(str, "static")
       || !strcmp(str, "struct") || !strcmp(str, "goto"))
       return (true);
   return (false);
}

// Checking for Integers..
bool isInt(char* str)
{
   int i, len = strlen(str);

   if (len == 0)
       return (false);
   for (i = 0; i < len; i++)

{
       if (str[i] != '0' && str[i] != '1' && str[i] != '2'
           && str[i] != '3' && str[i] != '4' && str[i] != '5'
           && str[i] != '6' && str[i] != '7' && str[i] != '8'
           && str[i] != '9' || (str[i] == '-' && i > 0))
           return (false);
   }
   return (true);
}

// Extracts the substring from the main string
char* subStr(char* str, int left, int right)
{
   int i;
   char* subStr = (char*)malloc(sizeof(char) * (right - left + 2));

   for (i = left; i <= right; i++)
       subStr[i - left] = str[i];
   subStr[right - left + 1] = '\0';
   return (subStr);
}

// Function for Parsing the Input String.
void parse(char* str)
{
   int left = 0, right = 0;
   int len = strlen(str);

   while (right <= len && left <= right) {
       if (isDelim(str[right]) == false)
           right++;

       if (isDelim(str[right]) == true && left == right) {
           if (isOp(str[right]) == true)
               printf("'%c' Is an operator \n", str[right]);

           right++;
           left = right;
       } else if (isDelim(str[right]) == true && left != right
               || (right == len && left != right)) {
           char* subStr = subStr(str, left, right - 1);

           if (isKey(subStr) == true)
               printf("'%s' Is a keyword \n", subStr);

           else if (isInt(subStr) == true)
               printf("'%s' Is an integer \n", subStr);

           else if (isValidId(subStr) == true
                   && isDelim(str[right - 1]) == false)
               printf("'%s' Is valid identifier\n", subStr);

           else if (isValidId(subStr) == false
                   && isDelim(str[right - 1]) == false)
               printf("'%s' Ii not a valid Identifier\n", subStr);
           left = right;
       }
   }
   return;
}

// Main Function
int main()
{
   // maximum legth of string is 100 here
   char str[100] = "int a = b + 1c; ";

   parse(str); // calling the parse function

   return (0);
}

The above program prints all individual tokens from an input stream. It is a bit lengthy but a basic one.

The subStr() function, can also be written using strtok() method, where delimiters and keywords need to be passed as argurment.

Hope this helps.

Do leave positive rating.

Add a comment
Know the answer?
Add Answer to:
For part one of this assignment, write a program (parse.c) that contains a function to parse...
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
  • Write a C program that parses a string (command line) and tokenize it by breaking the...

    Write a C program that parses a string (command line) and tokenize it by breaking the string characters into words that are separated by delimiters that can be white spaces (space and/or tab characters). It also must report how many commands in the input. This program will be reused for your next C programming assignment that is a simple shell interpreter/program called "tech shell". % a.out please enter a string: this is a test 1: this 2: is 3: a...

  • This project consists of designing a C program to serve as a shell interface that accepts...

    This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered. The example below illustrates the prompt cse222> and the user’s next command: cat prog.c. cse222> cat prog.c One technique for implementing a shell interface is to have the parent process first read what the user enters on the...

  • Overview Writing in the C language, implement a very basic shell, called smash. In this project,...

    Overview Writing in the C language, implement a very basic shell, called smash. In this project, we will work on processing strings and using the appropriate system calls. Build System setup Before we can start writing code we need to get our build system all setup. This will involve writing a very simple makefile. You should leverage your Makefile project for this part of the assignment! Write a Makefile that provides the three targets listed below all - The default...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • Please help me with the coding for LL(1)!! The given grammar was: P → PL |...

    Please help me with the coding for LL(1)!! The given grammar was: P → PL | L L → N; | M; | C N → print E M → print "W" W → TW | ε C → if E {P} | if E {P} else {P} E → (EOE) | V (note: this has a variable O) O → + | - | * V → 0 | 1 | 2 | 3 (note: this has a terminal...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • I need to construct a c++ class that defines a parser. This parser will be able...

    I need to construct a c++ class that defines a parser. This parser will be able to break up a string into tokens. A token is a distinct string of text – it can be a word, a symbol, or a combination of these. Your parser will eliminate whitespace and break string snippets into individual tokens (where single-character tokens are defined at runtime). Your parser should behave as follows: 1) Whitespace (spaces, tabs, and new lines) is used only to...

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Project 1: Implementing a Shell 1 Overview In this individual project you will have to design...

    Project 1: Implementing a Shell 1 Overview In this individual project you will have to design and implement a simple shell command interpreter called mysh. The basic function of a shell is to accept lines of text as input and execute programs in response. The shell must be able to execute built-in commands in a process different from the one executing mysh. 2 Requirements When first started, your shell should initialize any necessary data structures and then enter a loop...

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