1/*2 * Copyright © 2016-2018 Sören Tempel3 *4 * This program is free software: you can redistribute it and/or5 * modify it under the terms of the GNU Affero General Public6 * License as published by the Free Software Foundation, either7 * 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 of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12 * Affero General Public License for more details.13 *14 * You should have received a copy of the GNU Affero General Public15 * License along with this program. If not, see16 * <http://www.gnu.org/licenses/>.17 */1819#ifndef TMSIM_PARSER_H20#define TMSIM_PARSER_H2122#include <stdio.h>2324#include "scanner.h"25#include "token.h"26#include "turing.h"2728/**29 * A parser for the tmsim input format.30 */31typedef struct _parser parser;3233struct _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;3940 /**41 * Previous token returned by a call to next. Used to free the previous42 * token when requesting the next one.43 */44 token *prevtok;4546 /**47 * Current token, stored here instead in order to extract line and48 * column information form the token when the user requests an error49 * string.50 */51 token *tok;5253 /**54 * Underlying scanner for this parser.55 */56 scanner *scr;57};5859/**60 * Returned by various parser function to indicate what kind of error was61 * 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. */6667 PAR_STATEDEFTWICE, /**< State was defined twice. */68 PAR_TRANSDEFTWICE, /**< Non-deterministic turing machine. */6970 PAR_STARTKEY, /**< Parser didn't find 'start:' keyword. */71 PAR_INITALSTATE, /**< Parser didn't find an initial state. */7273 PAR_ACCEPTKEY, /**< Parser didn't encounter 'accept:' keyword. */74 PAR_NONSTATEACCEPT, /**< Invalid token in accepting state list. */7576 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. */7980 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;8687parser *newparser(char *, size_t);88parerr parsetm(parser *, dtm *);89void freeparser(parser *);90int strparerr(parser *, parerr, char *, FILE *);9192#endif