Skip to content

Latest commit

 

History

History
110 lines (90 loc) · 4.1 KB

README.md

File metadata and controls

110 lines (90 loc) · 4.1 KB

FJ-7825 Assignments

Zadanie 1: Arithmetic Expression Calculator

Description

An arithmetic calculator that parses and evaluates mathematical expressions with proper operator precedence. The calculator supports basic arithmetic operations including addition, subtraction, multiplication, division, and exponentiation. It uses lexical analysis (tokenization) and recursive descent parsing to evaluate expressions according to mathematical operator precedence rules.

Features

  • Operators supported: +, -, *, /, ^ (exponentiation)
  • Parentheses: Support for grouping expressions with ( and )
  • Unary operators: Support for unary plus and minus (e.g., -5, +3)
  • Operator precedence:
    • Highest: Exponentiation (^)
    • High: Multiplication (*), Division (/)
    • Low: Addition (+), Subtraction (-)
  • Interactive mode: Reads expressions from standard input
  • Error handling: Custom exception handling for invalid expressions

Gramatika v EBNF forme ZADANIE 1:

  1. Calc -> Statement EOF;
  2. Statement -> expr;
  3. expr -> mul{("+" | "-") mul};
  4. mul -> exp{("*" | "/") exp};
  5. exp -> term["^" exp];
  6. term -> NUMBER | "(" expr ")" | ("+" | "-") term;
  7. NUMBER -> digit{digit};
  8. digit -> "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";

Príklad odvodenia výrazu (3 * 3) - (3 * 3):

Calc => Statement EOF => expr EOF => mul - mul EOF => exp - exp EOF => term - term EOF 
     => (expr) - (expr) EOF => (mul) - (mul) EOF => (exp * exp) - (exp * exp) EOF 
     => (term * term) - (term * term) EOF => (NUMBER * NUMBER) - (NUMBER * NUMBER) EOF 
     => (3 * 3) - (3 * 3) EOF

Usage Example

Enter an expression (e.g., 1+2*3): (3 * 3) - (3 * 3)
0

Zadanie 2: State Machine Language Compiler

Description

A domain-specific language (DSL) compiler for defining and generating state machines. The program parses a custom state machine definition language and generates executable C code that implements the specified state machine behavior. This is useful for modeling systems with discrete states and transitions, such as controllers, protocols, or game logic.

Features

  • Custom DSL: Define state machines using an intuitive text-based language
  • Commands: Define output commands that the state machine can execute
  • Events: Define input events that trigger state transitions
  • Reset Commands: Specify commands that should be reset during transitions
  • States: Define states with associated actions and transitions
  • Code Generation: Automatically generates C code from the state machine definition
  • Lexer and Parser: Complete tokenization and parsing pipeline
  • Validation: Ensures state machine definition is syntactically and semantically correct

Gramatika v EBNF forme ZADANIE 2:

  1. StateMachine -> {Statement} EOF;
  2. Statement -> Commands | Events | ResetCommands | InitState | State
  3. Commands -> "commands" "{" { Command } "}";
  4. Command -> Identifier "=" CHAR_LITERAL;
  5. Events -> "events" "{" { Event } "}";
  6. Event -> Identifier "=" CHAR_LITERAL;
  7. ResetCommands -> "resetCommands" "{" { Identifier } "}";
  8. State -> "state" Identifier StateContent;
  9. StateContent -> "{" [Action] { Transition } "}";
  10. Action -> "actions" "[" { Identifier } "]";
  11. Transition -> Identifier "->" Identifier;
  12. Identifier -> letter { letter | digit };
  13. CHAR_LITERAL -> "'" character "'";
  14. letter -> "a" | "b" | ... | "z" | "A" | ... | "Z";
  15. digit -> "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
  16. character -> /* all printable characters */;
  17. EOF -> /* end of file token */;

Example State Machine Definition

commands {
    alarm             'a'
    openDoor          'o'
    closeDoor         'c'
}

resetCommands {
    alarm
    openDoor
}

events {
    safeUnlocked = 'U'
    safeLocked   = 'L'
    doorLocked   = 'C'
    doorUnlocked = 'O'
}

state initState {
    actions [doorUnlocked safeLocked]
    closeDoor --> active
}

Output

The compiler generates a C program (output.c) that implements the state machine logic, including state transitions, event handling, and command execution.