Question: / * * * * * * * * * * * * * * * * * * * * * * * *

/*
*********************************************
* Principles of Programming Languages *
* Fall 2024*
* Authors: Sheryl D'Mello *
*********************************************
*/
/*-------------------------------------------------
CFG for tinyL LANGUAGE
::=!
::=
::= ; |
::=||
::==
::=?
::=%
::=+|
|
|
& |
||
|
::= a | b | c | d | e | f
::=0|1|2|3|4|5|6|7|8|9
NOTE: tokens are exactly a single character long
Example expressions:
a=+2+25;%a!
a=|2&3|25;%a!
---------------------------------------------------
*/
#include
#include
#include
#include "Instr.h"
#include "InstrUtils.h"
#include "Utils.h"
#define MAX_BUFFER_SIZE 500
#define EMPTY_FIELD 0xFFFFF
#define token *buffer
/* GLOBALS */
static char *buffer = NULL; /* read buffer */
static int regnum =1; /* for next free virtual register number */
static FILE *outfile = NULL; /* output of code generation */
/* Utilities */
static void CodeGen(OpCode opcode, int field1, int field2, int field3);
static inline void next_token();
static inline int next_register();
static inline int is_digit(char c);
static inline int to_digit(char c);
static inline int is_identifier(char c);
static char *read_input(FILE * f);
/* Routines for recursive descending parser LL(1)*/
static void program();
static void stmtlist();
static void morestmts();
static void stmt();
static void assign();
static void read();
static void print();
static int expr();
static int variable();
static int digit();
/*************************************************************************/
/* Definitions for recursive descending parser LL(1)*/
/*************************************************************************/
static int digit()
{
int reg;
if (!is_digit(token))
{
ERROR("Expected digit
");
exit(EXIT_FAILURE);
}
reg = next_register();
CodeGen(LOADI, reg, to_digit(token), EMPTY_FIELD);
next_token();
return reg;
}
static int variable()
{
/* YOUR CODE GOES HERE */
int reg;
if (!is_identifier(token))
{
ERROR("Expected variable
");
exit(EXIT_FAILURE);
}
reg = next_register();
CodeGen(LOAD, reg, token, EMPTY_FIELD);
next_token();
return reg;
}
static int expr()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!