aboutsummaryrefslogtreecommitdiff
Lexical analysis

Lexical analysis

1. Task

Write a program that performs lexical analysis of the input text in accordance with the task and generates a table of tokens with indicating their types and values. Prepare several versions of the program in the form of text in the input language. The program should output messages indicating the presence of errors in the input text that may be detected during the lexical analysis stage. The length of identifiers and string constants is limited to 16 characters, using only Latin letters. The program must allow for comments of unlimited length in the input file (the format of the comments is selected independently). Construct syntax diagrams.

The input language contains loop statements for (...; ...; ...) do, separated by the | symbol (vertical bar). Loop statements contain identifiers, comparison operators <, >, =, string constants (a sequence of characters in double quotes), and an assignment operator. (:=).

Comment type:

  • // ... (until the end of the line)
  • /* ... */ (works with multiple lines and/or within a line)

2. Documentation

The exported report can be found at this link.

3. Showcase

$ ./build/lex
Usage: ./build/lex file
$ ./build/lex no/such/file
error: No such file or directory: no/such/file
$ ./build/lex res/01-correct-input.txt
Исходный текст:
for (i :=-.1230;
     i  < 10;
     i := 10)
do x := 11 "asdf" "asdf" "asdf"
|       // I don't know to work with those pipes...
for (x := 11; x = 1;) /* INLINE COMMENT */ "123456789ABCDE" /*
323asdf
MULTILINE COMMENT
  x=2      */
do x := 10

| *Лексема*        | *Тип лексемы*               | *Значение* |
|------------------+-----------------------------+------------|
| for              | Ключевое слово              | K[0]       |
| (                | Знак левой скобки           |            |
| i                | Идентификатор               | I[0]       |
| :=               | Знак присваивания           |            |
| -.1230           | Вещественная константа      | -0.123     |
| ;                | Знак точки с запятой        |            |
| i                | Идентификатор               | I[0]       |
| <                | Знак операции сравнения     | C[1]       |
| 10               | Целочиселная константа      | 10         |
| ;                | Знак точки с запятой        |            |
| i                | Идентификатор               | I[0]       |
| :=               | Знак присваивания           |            |
| 10               | Целочиселная константа      | 10         |
| )                | Знак правой скобки          |            |
| do               | Ключевое слово              | K[1]       |
| x                | Идентификатор               | I[1]       |
| :=               | Знак присваивания           |            |
| 11               | Целочиселная константа      | 11         |
| "asdf"           | Строковая константа         | S[0]       |
| "asdf"           | Строковая константа         | S[0]       |
| "asdf"           | Строковая константа         | S[0]       |
| |                | Символ вертикальной полосы  |            |
| for              | Ключевое слово              | K[0]       |
| (                | Знак левой скобки           |            |
| x                | Идентификатор               | I[1]       |
| :=               | Знак присваивания           |            |
| 11               | Целочиселная константа      | 11         |
| ;                | Знак точки с запятой        |            |
| x                | Идентификатор               | I[1]       |
| =                | Знак операции сравнения     | C[0]       |
| 1                | Целочиселная константа      | 1          |
| ;                | Знак точки с запятой        |            |
| )                | Знак правой скобки          |            |
| "123456789ABCDE" | Строковая константа         | S[1]       |
| do               | Ключевое слово              | K[1]       |
| x                | Идентификатор               | I[1]       |
| :=               | Знак присваивания           |            |
| 10               | Целочиселная константа      | 10         |

| *№* | *K* |
|-----+-----|
|   0 | for |
|   1 | do  |

| *№* | *C* |
|-----+-----|
|   0 | =   |
|   1 | <   |
|   2 | >   |

| *№* | *S*            |
|-----+----------------|
|   0 | asdf           |
|   1 | 123456789ABCDE |

| *№* | *I*              |
|-----+------------------|
|   0 | i                |
|   1 | x                |
$ ./build/lex res/02-incorrect-input.txt
Исходный текст:
dofor (i :=-.1230;
     i != 10;
     i := 10)
fordo x := 11ebc123 "asdfasdfasdfasdfasdfasdf"
|       // I don't know to work with those pipes...
for (x := 11; x = 1;) /* INLINE COMMENT */ "123456789ABCDE" /*
323asdf
MULTILINE COMMENT
  x=2      */
do _abcdefghijklmnop_1 := 10

/*

some more text

| *Лексема*        | *Тип лексемы*               | *Значение* |
|------------------+-----------------------------+------------|
| dofor            | Идентификатор               | I[0]       |
| (                | Знак левой скобки           |            |
| i                | Идентификатор               | I[1]       |
| :=               | Знак присваивания           |            |
| -.1230           | Вещественная константа      | -0.123     |
| ;                | Знак точки с запятой        |            |
| i                | Идентификатор               | I[1]       |
| =                | Знак операции сравнения     | C[0]       |
| 10               | Целочиселная константа      | 10         |
| ;                | Знак точки с запятой        |            |
| i                | Идентификатор               | I[1]       |
| :=               | Знак присваивания           |            |
| 10               | Целочиселная константа      | 10         |
| )                | Знак правой скобки          |            |
| fordo            | Идентификатор               | I[2]       |
| x                | Идентификатор               | I[3]       |
| :=               | Знак присваивания           |            |
| |                | Символ вертикальной полосы  |            |
| for              | Ключевое слово              | K[0]       |
| (                | Знак левой скобки           |            |
| x                | Идентификатор               | I[3]       |
| :=               | Знак присваивания           |            |
| 11               | Целочиселная константа      | 11         |
| ;                | Знак точки с запятой        |            |
| x                | Идентификатор               | I[3]       |
| =                | Знак операции сравнения     | C[0]       |
| 1                | Целочиселная константа      | 1          |
| ;                | Знак точки с запятой        |            |
| )                | Знак правой скобки          |            |
| "123456789ABCDE" | Строковая константа         | S[0]       |
| do               | Ключевое слово              | K[1]       |
| :=               | Знак присваивания           |            |
| 10               | Целочиселная константа      | 10         |

| *№* | *K* |
|-----+-----|
|   0 | for |
|   1 | do  |

| *№* | *C* |
|-----+-----|
|   0 | =   |
|   1 | <   |
|   2 | >   |

| *№* | *S*            |
|-----+----------------|
|   0 | 123456789ABCDE |

| *№* | *I*              |
|-----+------------------|
|   0 | dofor            |
|   1 | i                |
|   2 | fordo            |
|   3 | x                |
res/02-incorrect-input.txt:2:7: error: invalid lexeme `!'
    2 |      i != 10;
      |        ^
res/02-incorrect-input.txt:4:11: error: invalid number constant `11ebc123'
    4 | fordo x := 11ebc123 "asdfasdfasdfasdfasdfasdf"
      |            ^~~~~~~~
res/02-incorrect-input.txt:4:20: error: string constant longer than 16 symbols (26)
    4 | fordo x := 11ebc123 "asdfasdfasdfasdfasdfasdf"
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
res/02-incorrect-input.txt:10:3: error: identifier longer than 16 symbols (19)
   10 | do _abcdefghijklmnop_1 := 10
      |    ^~~~~~~~~~~~~~~~~~~
res/02-incorrect-input.txt:12:0: error: unterminated comment
   12 | /*
      | ^~

4. Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

Date: 2025H1

Author: Martin Michalec

Created: 2026-11-08 Sun 08:13