Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions gradings/labs/Timur Asapov/lang_c++/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eof.c
16 changes: 16 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.10)
project(lab1 C)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Wshadow")

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")

set(BUILD_1 main)
set(SOURCE_FILES_1 demo.c src/parcer.c)
add_executable(${BUILD_1} ${SOURCE_FILES_1})

enable_testing()

add_subdirectory(tests)
44 changes: 44 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/CMakeModules/FindCheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# - Try to find the CHECK libraries
# Once done this will define
#
# CHECK_FOUND - system has check
# CHECK_INCLUDE_DIRS - the check include directory
# CHECK_LIBRARIES - check library
#
# Copyright (c) 2007 Daniel Gollub <dgollub@suse.de>
# Copyright (c) 2007 Bjoern Ricks <b.ricks@fh-osnabrueck.de>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

INCLUDE( FindPkgConfig )

# Take care about check.pc settings
PKG_SEARCH_MODULE( CHECK check )

# Look for CHECK include dir and libraries
IF( NOT CHECK_FOUND )

FIND_PATH( CHECK_INCLUDE_DIRS check.h )

FIND_LIBRARY( CHECK_LIBRARIES NAMES check )

IF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
SET( CHECK_FOUND 1 )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
ENDIF ( NOT Check_FIND_QUIETLY )
ELSE ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
IF ( Check_FIND_REQUIRED )
MESSAGE( FATAL_ERROR "Could NOT find CHECK" )
ELSE ( Check_FIND_REQUIRED )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE( STATUS "Could NOT find CHECK" )
ENDIF ( NOT Check_FIND_QUIETLY )
ENDIF ( Check_FIND_REQUIRED )
ENDIF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
ENDIF( NOT CHECK_FOUND )

# Hide advanced variables from CMake GUIs
MARK_AS_ADVANCED( CHECK_INCLUDE_DIRS CHECK_LIBRARIES )
396 changes: 396 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/README.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by cubazis on 01.09.18.
//

#include <stdio.h>
#include "src/parcer.h"

int main(void)
{
file = fopen( "in.txt" , "r");
FILE * out = fopen( "out.txt" , "w");
statements();
printf("%s\n", parser_result);
fprintf(out, parser_result);

fclose(file);
fclose(out);
}
1 change: 1 addition & 0 deletions gradings/labs/Timur Asapov/lang_c++/in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1+2;3*2+1;
1 change: 1 addition & 0 deletions gradings/labs/Timur Asapov/lang_c++/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NUM PLUS NUM SEMI NUM TIMES NUM PLUS NUM SEMI EOF
174 changes: 174 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/src/parcer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//
// Created by cubazis on 27.08.18.
//

#include "parcer.h"
#include "string.h"

/** Variables for inter communication of lexical analyzer and parser */

char *yytext = ""; /* Lexeme (not '\0' terminated) */
int yyleng = 0; /* Lexeme length. */
int yylineno = 0; /* Input line number */

void* lex()
{
static char buffer[128];
char *current;

current = yytext + yyleng; /* Skip current lexeme */

while( 1 ) /* Get the next one */
{
while( !*current )
{
/* Get new lines, skipping any leading white space on the line,
* until a nonblank line is found.
*/

current = buffer;
if( !fgets( buffer, sizeof(buffer), file) )
{
*current = '\0' ;
return _EOI;
}
++yylineno;

while( isspace(*current) )
++current;
}

for( ; *current ; ++current )
{
/* Get the next token */

yytext = current;
yyleng = 1;

switch( *current )
{
case ';': return (void *) _SEMI;
case '+': return (void *)_PLUS ;
case '*': return (void *)_TIMES ;
case '(': return (void *)_LP ;
case ')': return (void *)_RP ;

case '\n':
case '\t':
case ' ' : break;

default:
if( !isdigit(*current) ){
//fprintf(stderr, "Ignoring illegal input <%c>\n", *current);
return (void *)_ERR_1;
}
else
{
while( isdigit(*current) )
++current;

yyleng = (int) (current - yytext);
return (void *)_NUM;
}
}
}
}
}

static int Lookahead = -1; /* Lookahead token */

int match(int token )
{
/* Return true if "token" matches the current lookahead symbol. */

if( Lookahead == -1 )
Lookahead = (int) lex();

return token == Lookahead;
}

void advance()
{
/* Advance the lookahead to the next input symbol. */

Lookahead = (int) lex();
}

void expression()
{
/* expression -> term expression' */
term();
expr_prime();
}

void term()
{
/* term -> factor term' */

factor();
term_prime();
}

void expr_prime()
{
/* expression' -> PLUS term expression'
* | epsilon
*/
if (match(_PLUS)) {
strcat(parser_result, "PLUS ");
advance();
term();
expr_prime();
}
/** YOUR CODE HERE */
}

void term_prime()
{
/* term' -> TIMES factor term'
* | epsilon
*/
if (match(_TIMES)) {
strcat(parser_result, "TIMES ");
advance();
factor();
term_prime();
}
}

void factor()
{
/* factor -> NUM_OR_ID
* | LP expression RP
*/
if (match(_NUM)) {
strcat(parser_result, "NUM ");
advance();
}
if (match(_LP)) {
strcat(parser_result, "LP ");
advance();
expression();
strcat(parser_result, "RP ");
advance();
}
}

void statements()
{
/* statements -> expression SEMI
* | expression SEMI statements
*/
expression();

if( match( _SEMI ) ){
strcat(parser_result, "SEMI ");
advance();
}

if( !match(_EOI) ){
statements(); /* Do another statement. */
} else {
strcat(parser_result, "EOF");
}
}
51 changes: 51 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/src/parcer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Created by cubazis on 27.08.18.
//

#ifndef LAB1_PARCER_H
#define LAB1_PARCER_H

#include <stdio.h>
#include <ctype.h>

/** parser internal entity to process the input stream */
FILE * file;

/** parser internal entity to provide parsing result*/
char parser_result[1000];

/** Tokens set */
#define _EOI 0 /* end of input (EOF signal typically) */
#define _SEMI 1 /* ; */
#define _PLUS 2 /* + */
#define _TIMES 3 /* * */
#define _LP 4 /* ( */
#define _RP 5 /* ) */
#define _NUM 6 /* decimal number */

#define _ERR_1 "Ignoring illegal input"

/**
* lexical analyzer function
* @return token (int number from the token set)
*/

void* lex();

int match( int token );

void advance();

void expression();

void term();

void expr_prime();

void factor();

void term_prime();

void statements();

#endif //LAB1_PARCER_H
14 changes: 14 additions & 0 deletions gradings/labs/Timur Asapov/lang_c++/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
enable_testing()

find_package(Check REQUIRED)
find_package(Threads REQUIRED)

include_directories(${CHECK_INCLUDE_DIRS})

include_directories(. ../src)

add_executable(test_parcer test_parcer.c ../src/parcer.c)

target_link_libraries(test_parcer ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

add_test(test_parcer ${CMAKE_CURRENT_BINARY_DIR}/test_parcer)
Loading