0% found this document useful (0 votes)
10 views

PCD - ALGO

The document outlines various algorithms for constructing NFAs and DFAs from regular expressions, implementing lexical analyzers, and performing arithmetic expression evaluations using YACC and recursive descent parsing. It also describes methods for counting characters, words, vowels, and numbers, as well as generating intermediate code and optimizing it. Additionally, it includes procedures for building symbol tables and generating assembly code from postfix expressions.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PCD - ALGO

The document outlines various algorithms for constructing NFAs and DFAs from regular expressions, implementing lexical analyzers, and performing arithmetic expression evaluations using YACC and recursive descent parsing. It also describes methods for counting characters, words, vowels, and numbers, as well as generating intermediate code and optimizing it. Additionally, it includes procedures for building symbol tables and generating assembly code from postfix expressions.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

NFA Construction
Aim:
To construct a Non-deterministic Finite Automaton (NFA) for a given
regular expression.
Algorithm:
1. Initialize the transition table.
2. Read the regular expression.
3. Traverse each character:
o Handle a, b, and e transitions.
4. For * and /, apply looping and branching using epsilon.
5. Use state numbers to track transitions.
6. Print the final NFA transition table.

2. Regular Expression to DFA


Aim:
To convert a regular expression into an equivalent Deterministic
Finite Automaton (DFA).
Algorithm:
1. Input the regular expression.
2. Traverse the expression and separate operands and operators.
3. Handle * and + appropriately.
4. Construct DFA states and transitions.
5. Represent them in a transition table.
6. Display the DFA table.
3. Lexical Analyzer using Lex
Aim:
To implement a lexical analyzer using Lex that identifies tokens in a
program.
Algorithm:
1. Define patterns for keywords, identifiers, operators, and
integers.
2. Write Lex rules to recognize each token.
3. Skip whitespaces and handle invalid input.
4. Compile using lex and generate C code.
5. Provide input and call yylex().
6. Print matched token types.

4. Count Characters, Words, and Lines using Lex


Aim:
To count characters, words, and lines in a given file using Lex.
Algorithm:
1. Define rules for:
o New lines
o Words
o Letters, digits, and special characters
2. Increment counters for each match.
3. Read from a file using fopen().
4. Call yylex() to perform scanning.
5. Sum up all counts.
6. Display the final results.

5. Count Vowels and Consonants using Lex


Aim:
To count vowels and consonants in a given string using Lex.
Algorithm:
1. Define vowel pattern [aeiouAEIOU].
2. Define consonant pattern [a-zA-Z].
3. On vowel match, increment vowel count.
4. On consonant match, increment consonant count.
5. Use yylex() to read and match input.
6. Print total vowels and consonants.

6. Count Positive and Negative Numbers using Lex


Aim:
To count positive and negative numbers and fractions using Lex.
Algorithm:
1. Define patterns for:
o Positive/negative integers
o Positive/negative decimals
2. On match, increment respective counters.
3. Ignore irrelevant input.
4. Use yylex() to scan input.
5. Count and display results.
6. End program with summary.

7. Arithmetic Expression Evaluation using YACC


Aim:
To evaluate arithmetic expressions using YACC.
Algorithm:
1. Define grammar rules in YACC.
2. Set precedence for operators.
3. Write semantic actions for each rule.
4. Call yyparse() to parse input.
5. On valid expression, compute and display result.
6. On error, call yyerror().

8. Recursive Descent Parser


Aim:
To implement a recursive descent parser for expressions.
Algorithm:
1. Read the input expression.
2. Define functions: E(), Edash(), T(), Tdash(), F().
3. Start with E().
4. Match symbols recursively and validate grammar.
5. Print production rules used.
6. Accept if input is parsed fully.
9. Operator Precedence Parser
Aim:
To parse input using operator precedence parsing.
Algorithm:
1. Input terminals and precedence table.
2. Initialize stack and input string.
3. Shift input symbols based on < or =.
4. Reduce stack when > is found.
5. Repeat until input is parsed.
6. Accept or reject based on final stack.

10. Symbol Table Construction


Aim:
To build a symbol table from an expression.
Algorithm:
1. Read an expression ending with $.
2. Traverse each character.
3. Identify letters as identifiers.
4. Identify operators (+, -, *, etc.).
5. Allocate memory and store address/type.
6. Display the symbol table.
11. Shift-Reduce Parser
Aim:
To simulate shift-reduce parsing.
Algorithm:
1. Read production rules and input string.
2. Shift input symbols onto the stack.
3. On match, reduce using production rule.
4. Display stack and action after each step.
5. Continue until input is accepted.
6. Reject if parsing fails.

12. LR Parser
Aim:
To simulate a simple LR parsing process.
Algorithm:
1. Read the arithmetic expression.
2. Scan input and shift id as E.
3. Push operators onto stack.
4. Perform reductions using grammar rules.
5. Print stack after each step.
6. Accept if reduced to start symbol.
13. Intermediate Code Generation
Aim:
To generate intermediate (three-address) code from expressions.
Algorithm:
1. Read an arithmetic expression.
2. Identify operators by precedence.
3. Extract left and right operands.
4. Assign temporary variables.
5. Replace original expression with temp results.
6. Print three-address code.

14. Code Optimization


Aim:
To optimize intermediate code by removing dead and common code.
Algorithm:
1. Input intermediate code.
2. Identify and eliminate dead code.
3. Detect common sub-expressions.
4. Replace them with single computation.
5. Update dependent instructions.
6. Display optimized code.

15. Code Generation


Aim:
To generate assembly code from postfix expressions.
Algorithm:
1. Read postfix expression.
2. Use a stack to evaluate it.
3. For each operator:
o Pop two operands
o Generate assembly instructions
4. Store result in temporary variable.
5. Push result back to stack.
6. Display final result location.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy