Question
I'm having trouble converting the below pseudo-code, I don't really get what the procedure stuff is all about. Is it supposed to be a struct
I'm having trouble converting the below pseudo-code, I don't really get what the procedure stuff is all about. Is it supposed to be a struct or is it something in the stdlib? I've also added the full assignment for more context and what code I've got so far:
Project Description
You will implement this program in the C programming language. You must provide a makefile and your program must run on the ssh server in Linux. Your program will implement the pseudocode described in class as needed, adding, removing or editing elements as needed. Your application must take one command-line argument, the name of the file to be checked. The file will contain a number of assignment statements. Each assignment statement will be terminated by a semicolon (;). Furthermore, your group of assignment statements must be delimited by the "begin" and "end" reserved words with a dot (.) after the "end" token. The intent is that programs will be free format, meaning that indentations do not matter, multiple statements are permitted on a line, a statement may appear on multiple lines (with no continuation character). Finally, your program must include a capability to provide comments. Comments will be on a single line and they will start with a tilda:
~ this is a comment
You will parse the statements, keeping track of where you are in the string, and report whether or not the input program is legal. Your program will report either
- The program is legal: "success". - Line
If all statements are legal, you must report all the identifiers that were used. Therefore, you will need at symbol table. Your symboltable can be implemented however you wish, but a hashtable is the logical choice. If the program is illegal, you do not need to list the identifiers.
You will implement both a lexical analyzer and a parser. These are clearly different functions and they should be implemented in different physical files. The lexical analyzer will simply decide what comes next in the token stream and return it. The parser will decide if the token is OK at that spot.
In your program, you will need to have an
int lookahead;
that always holds the next token.
The structure of Number and Identifier
You should detect whether or not a character string comprises an integer.
These are valid numbers: 123, 1234567, 456789, etc ...
A legal
1. You cannot have consecutive underscores
2. An identifier cannot end with an underscore.
These are valid identifiers: e123, e, qwert5yuio, a_b_7
These are not:
e__7, abc_, 7yght, _iuh
Typical Statements (legal and illegal)
a = b; a=af+= a=; a_5 = 2; 6 = j; a=d* dd5=((3+5*8); %iii=0 a=b2-(((x_yz + 99)* abc)*d); qwerty=((6 - abcd)*(a + b)); xyz = (9*8*7*6)/(a+b+c);
A legal program
~ a typical, minimal legal program begin qwerty=((6 - ab_cd)*(a + b)); xyz = (9*8*7765*6)/(a+b+c); end
My Code:
main.c
========================================================================================
#include
int main(int argc, char* argv[]) { parse(argv[1]); }
========================================================================================
parse.c
=========================================================================================
#include "parse.h"
void parse(char* str) { printf("nparse %s ",str); fp = fopen(str,"r"); parseFile(fp, ";"); }
void parseFile(FILE *fp, char* delimiter) { }
void showLexemes() { }
int lookup(char s[]) { }
void match(int t, char * message) { if(lookahead == t) { lookahead = lexan(); }else { printf("syntax error"); } }
=========================================================================================
parse.h
==========================================================================================
#ifndef PARSE_H #define PARSE_H #include
int tokenVal; int lineNo; int lookahead;
FILE * fp;
struct entry { char *lexPtr; int token; };
void parse(char*); void match(int t, char * message); int lookup(char s[]); void showLexemes(); void parseFile(FILE *fp, char* delimiter); #endif
======================================================================================
lexer.h
=======================================================================================
#ifndef LEXER_H #define LEXER_H #include "parse.h" #include
int tokenVal; int lineNo; int lookahead;
int lexan(); int lookup(char s[]);
#endif
============================================================================================
lexer.c
=======================================================================================
#include "lexer.h"
int lexan() { char ch; //find BEGIN while(1) { ch = getchar(); if(ch == ' ' || ch == '\t') { ;//do nothing }else if(ch == ' ') { lineNo++; }else if(isdigit(ch)) { printf("found digit"); /*begin get the number into numLexeme return NUM; end */ }else if(isalpha(ch)) { printf("found a letter"); /*begin get the identifier into idlexeme typer = lookup(ID); if(type == NOT_FOUND) { insert value into symbolTable return ID; }else { return type; } end */ }else if(ch == EOF) { return DONE; }else { return ch; } } }
===============================================================================
symbol.h
===============================================================================
#ifndef SYMBOL_H #define SYMBOL_H #include "lexer.h"
struct entry symTable[1000]; char expression[500];
#endif
==============================================================================
symbol.c
=============================================================================
#include "symbol.h"
If possible please help me convert this pseudo-code to c code:
An outline of a Lexical Analyzer int procedure lexan begin while(true) ch = getchar(); if (ch is a space or ch is a tab) ; // do nothing else if (ch is a newline) increment lineno; else if (ch is a digit) begin get the number into numLexeme return NUM; end else if (ch is a letter) begin get the identifier into idLexeme type = lookup (idLexeme); if (type NOT_FOUND) insert value into symbolTable return ID else return type; end else if (ch EOF) return DONE; else return ch endwhile end lexan == = Outline for a Simple Recursive Descent Parser procedure AssignStmt begin match (ID) if (lookahead '=') then error else begin match(lookahead); expression; match(';'); end; end; '-') procedure expression; begin term; while (lookahead '+' or lookahead begin match (lookahead); term; end; end; Recursive Descent Parser I* or lookahead = '/') procedure term; begin factor while (lookahead = begin match (lookahead); factor; end; end; == procedure factor; begin if (lookahead ID) then match (ID); else if lookahead NUM) then match (NUM); else if lookahead '(' then begin match('('); expression; match(')'); end; else error; end; Recursive Descent Parser == procedure match(int t, char * message) { if(lookahead t) lookahead lexan(); else error ("syntax error"); } An outline of a Lexical Analyzer int procedure lexan begin while(true) ch = getchar(); if (ch is a space or ch is a tab) ; // do nothing else if (ch is a newline) increment lineno; else if (ch is a digit) begin get the number into numLexeme return NUM; end else if (ch is a letter) begin get the identifier into idLexeme type = lookup (idLexeme); if (type NOT_FOUND) insert value into symbolTable return ID else return type; end else if (ch EOF) return DONE; else return ch endwhile end lexan == = Outline for a Simple Recursive Descent Parser procedure AssignStmt begin match (ID) if (lookahead '=') then error else begin match(lookahead); expression; match(';'); end; end; '-') procedure expression; begin term; while (lookahead '+' or lookahead begin match (lookahead); term; end; end; Recursive Descent Parser I* or lookahead = '/') procedure term; begin factor while (lookahead = begin match (lookahead); factor; end; end; == procedure factor; begin if (lookahead ID) then match (ID); else if lookahead NUM) then match (NUM); else if lookahead '(' then begin match('('); expression; match(')'); end; else error; end; Recursive Descent Parser == procedure match(int t, char * message) { if(lookahead t) lookahead lexan(); else error ("syntax error"); }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started