Question

Note: The question needs to be answered in "C Programming Languange ". And after the question find 3 pages for needed informations.

Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5;


The instruction set for the stack machine Add [SP-11-SP-1]+[SP): SP ← SP-1 Addition. Pop the top two stack elements, add the

Spring 2015 CE4717 Store [SP1+2 Dec 4 store (sP1+lofset) SP SP (words) Decrement the stack pointer. Remove (words) words of

The grammar for the compiler project language (Program) PROGRAM (ldentifier) [ (Declarations)] [ (ProcDeciaration) (Block)

Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5; return R6; IA-2a-z)[A-Za-z0-9]- -2 10-91+ 10-9a-EA-FI Ihi] [01] [01] 이삐 t Vtin) int main (void) int tcode; do f tcode -yylex()i printf ("token type td \"%s\"\n", tcode, yytext); ) while (tcode)i return 0; i. Explain the steps needed to convert this source file to an executable program using FLEX under Unix/Linux. ii. Explain what rules 2...6 are supposed to achieve in the above program. (N.B., Rules 2 ...4 are standard FLEX idioms, rules 5 and 6 are not but their meanings should be clear.) ili. The above program is missing some syntax and also contains a number of logical errors. Explain all the errors and rewrite the program so that it is syntactically correct and runs properly iv. Parse the following input string 1.1b1.10b245hi using your rewitten LEX program
The instruction set for the stack machine Add [SP-11-SP-1]+[SP): SP ← SP-1 Addition". Pop the top two stack elements, add them and push the Bz 200 Bz (addr) if [SPJ-0 then PC-addr); SP ← SP-1 Example: Add "Branch if equal to Zero". Branch to memory address (addr) if the result back on the stack top of stack is equal to zero, Pop the stack. Bnz (addr) if[SP)メ○then PC ←(addr): SP ← SPー1 Sub Sub Bnz 100 Subtraction". Subtract the top of stack from the next ciement on the stack. Two stack elements are popped and the result is pushed back Muit Branch if not equal to Zero". Branch to memory address (addr) if the top of stack is not equal to zero. Pup the stack. Call (addr SP SP+I: [SP] PC+I: PC+ (addr): "Call a subroutine". First push the address of the instruction follow Mult Call 234 Multiplication". Pop the top two stack elements, multiply thern and push the result back on the stack. ing the Cal1 onto the stack, then jump to (addr). Div Div Ret Ret Return from a subroutine". Pop the top of stack and place its value in the program counter Division". Divide the top of stack by the next element on the stack. Two stack elenents are popped and the result is pushed back. Neg [SP) ←-[SPf Neg "Build Stack Frame". Prepare to enter a subroutine by building the dynamic link portion of a slack frame. Rsf Negation". Negate the top of stack, i.e., replace it with its two's Br 200 Rsf PC -(addr) "Branch to address". Branch to memory address (addr) Remove Stack Frame", Remove a stack frame on exiting a subroa- tine. Restore the Frame Pointer to point to the base of the previous stack frame. Bgz 200 if ISP12 0 then PC +-(addr): SP SP-i "Branch if Greater than or equal to Zero. Branch to memory address (addr) if the top of stack is greater than or equal to zero. Pop the stack Ldp 103 "Load Display Pointer". Copy the old display pointer in (addr) to the location pointed to by the Frame Pointer, then replace it with the current value of the Frame Puinter B4 Bg 200 if (SP]> 0 then PC(adr); SP SP-1 Branch if Greater than zero". Branch to memory address (addr) if the top of stack is greater than zero. Pop the stack. Rdp (addr) Rdp 103 "Restore old Display Pointer". Replace the display pointer currentdy at (addr) with the previously active display pointer, currently pre- served in the location pointed at by the Frame Pointer Blz (addr) Blz 200 if ISP) S 0 then PC(addr): SP SP-1 "Branch if Less than or equal to Zero". Branch to memory address (addr) if the top of stack is less than or equal to zero. Pop the stack. Inc (words) Inc 4 SP SP+(words) Increment the stack pointer". Create (words) words of local vari- able space on the stack Bi 200 if (SPI
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lex source program Lex compiler lex.yy.c lex.yy.c C compiler a.out input a.out tokens

1

i. Save your program under the extension ".l" l stands for lex

the next command to be executed is " flex <programname.l>

for example if your program name is sample.l

then execute flex sample.l

this will create a file lex.yy.c

As the extension indicates it is a 'C ' file

To compile lex.yy.c type gcc lex.yy.c

to run the program ./a.out

ii) rule number 2 is for identifiers it should start with alphabet followed by any number of alphabets or digits

r3- for exponential number with optional sign bit. we can have negative numbers

r4 is for integers with optional negative sign

r5 is for hexadecimal number

r6 is for binary number in the format 0101,1010 but without any decimal point

iii)

%option noyywrap
%{
   #include<stdio.h>
%}
%%
[0-9]               {printf("Valid Identifier");}
-?[0-9]+(\.[0-9]+)?([eE][+|-]?[0-9]+)?            {printf("Valid exponent");}
[0-9a-fA-F]+[hH]       {printf("hex number");}
[01]\.?[01]*[bB]           {printf("binary");}
-?[0-9]+               {printf("number");}
[\t\n]               ;

%%
int main ()
{
int tcode;
do{
tcode= yylex();
printf("enter the value");
printf ("token type %d \"%s\"\n", tcode, yytext);

} while(tcode);
   }

rule for binary has been slightly modified to include '.'

when the input is 1.1b1.10b245hi

what we get is binary binary hex

Valid Identifier 23.9 Ualid exponent .1b1.10b245hi

Add a comment
Know the answer?
Add Answer to:
Note: The question needs to be answered in "C Programming Languange ". And after the question fin...
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
  • SUBROUTINES Example 2: May 2006 Question 2 The main program uses a subroutine located at address...

    SUBROUTINES Example 2: May 2006 Question 2 The main program uses a subroutine located at address 3000,- Read the program and answer the following questions. (a) What is the address of the instruction DCR E in the main program?[3 marks] (b) What instructions need to be written in the subroutine and give the address of the instructions? [3 marks] List the contents of the stack, the PC, and the stack pointer after (i) Execution of CALL instruction in the main...

  • LC-3 Programming Help!! The Stack Protocol The following outline is the protocol for passing arguments to...

    LC-3 Programming Help!! The Stack Protocol The following outline is the protocol for passing arguments to a function and returning values. Everything is stored on the runtime stack so that space is used only when the function is executing. As a result the actual address of arguments and locals may change from call to call. However, the layout of the stack frame (activation record) is constant. Thus, the offests from the frame pointer (FP) to the parameters/locals are constant. All...

  • Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how...

    Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise...

    Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise as a practice for c programming, could someone do this as well so i could compare if my code makes makes sense and to see and help correct the errors im getting right now.. Any helpful help would be appreciated.. Pointers &Pointer arithmetic Memory allocation and freeing Main topics: Exercise This lab is designed to give you practice working with pointers and memory allocation...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin ...

    LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin reserved section: do not change ANYTHING in reserved section! .ORIG x3000 BR Main ; Parameter and result Param .FILL x0004 Result .BLKW 1 ; Constants Stack .FILL x4000 One .FILL #1 MinusOne .FILL #-1 ; End reserved section: do not change ANYTHING in reserved section! ;------------------------------------------------------------------------------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; int Factorial(int N) ; Returns N! (must be a recursive function) ; Factorial ;__________________...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

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