tmsim

A fast turing machine simulator with graphviz export functionality

git clone https://git.8pit.net/tmsim.git

 1/*
 2 * Copyright © 2016-2018 Sören Tempel
 3 *
 4 * This program is free software: you can redistribute it and/or
 5 * modify it under the terms of the GNU Affero General Public
 6 * License as published by the Free Software Foundation, either
 7 * version 3 of the License, or (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef TMSIM_PARSER_H
20#define TMSIM_PARSER_H
21
22#include <stdio.h>
23
24#include "scanner.h"
25#include "token.h"
26#include "turing.h"
27
28/**
29 * A parser for the tmsim input format.
30 */
31typedef struct _parser parser;
32
33struct _parser {
34	/**
35	 * Special token used to enable peeking functionality for tokens.
36	 * Needed because we can't peek the next token from a concurrent queue.
37	 */
38	token *peektok;
39
40	/**
41	 * Previous token returned by a call to next. Used to free the previous
42	 * token when requesting the next one.
43	 */
44	token *prevtok;
45
46	/**
47	 * Current token, stored here instead in order to extract line and
48	 * column information form the token when the user requests an error
49	 * string.
50	 */
51	token *tok;
52
53	/**
54	 * Underlying scanner for this parser.
55	 */
56	scanner *scr;
57};
58
59/**
60 * Returned by various parser function to indicate what kind of error was
61 * encountered. Makes it easier to debug syntax errors in input files.
62 */
63typedef enum {
64	PAR_OK,        /**< Input was parsed successfully. */
65	PAR_SEMICOLON, /**< Parser didn't encounter semicolon. */
66
67	PAR_STATEDEFTWICE, /**< State was defined twice. */
68	PAR_TRANSDEFTWICE, /**< Non-deterministic turing machine. */
69
70	PAR_STARTKEY,    /**< Parser didn't find 'start:' keyword. */
71	PAR_INITALSTATE, /**< Parser didn't find an initial state. */
72
73	PAR_ACCEPTKEY,      /**< Parser didn't encounter 'accept:' keyword. */
74	PAR_NONSTATEACCEPT, /**< Invalid token in accepting state list. */
75
76	PAR_STATEDEF, /**< Expected statename for state definition. */
77	PAR_LBRACKET, /**< Missing opening left bracket in state definition. */
78	PAR_RBRACKET, /**< Missing closing right bracket in state definition. */
79
80	PAR_RSYMBOL,      /**< Expected symbol to read for transition. */
81	PAR_DIRECTION,    /**< Expected direction symbol for head movement. */
82	PAR_WSYMBOL,      /**< Expected symbol to write after transition. */
83	PAR_NEXTSTATESYM, /**< Expected '=>' symbol. */
84	PAR_NEXTSTATE,    /**< Expected name of new state. */
85} parerr;
86
87parser *newparser(char *, size_t);
88parerr parsetm(parser *, dtm *);
89void freeparser(parser *);
90int strparerr(parser *, parerr, char *, FILE *);
91
92#endif