Skip to content

Commit bab7295

Browse files
committed
Integrate ST parser into CBMC executable
Includes a basic ST language interface (although without any typechecking yet) as well as modifications to some Makefiles and cbmc_languages.cpp. Use the --show-parse-tree option when parsing a .awl file to see the project's output.
1 parent 3f4585f commit bab7295

16 files changed

+643
-2
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ add_subdirectory(langapi)
9595
add_subdirectory(linking)
9696
add_subdirectory(pointer-analysis)
9797
add_subdirectory(solvers)
98+
add_subdirectory(structured-text)
9899
add_subdirectory(util)
99100
add_subdirectory(xmllang)
100101

src/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ DIRS = analyses \
2020
memory-analyzer \
2121
pointer-analysis \
2222
solvers \
23+
structured-text \
2324
symtab2gb \
2425
util \
2526
xmllang \
@@ -60,7 +61,7 @@ cpp.dir: ansi-c.dir linking.dir
6061

6162
languages: util.dir langapi.dir \
6263
cpp.dir ansi-c.dir xmllang.dir assembler.dir \
63-
jsil.dir json.dir json-symtab-language.dir
64+
jsil.dir json.dir json-symtab-language.dir structured-text.dir
6465

6566
solvers.dir: util.dir
6667

src/cbmc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ target_link_libraries(cbmc-lib
2323
linking
2424
pointer-analysis
2525
solvers
26+
structured-text
2627
util
2728
xml
2829
)

src/cbmc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ OBJ += ../ansi-c/ansi-c$(LIBEXT) \
4141
../xmllang/xmllang$(LIBEXT) \
4242
../assembler/assembler$(LIBEXT) \
4343
../solvers/solvers$(LIBEXT) \
44+
../structured-text/structured-text$(LIBEXT) \
4445
../util/util$(LIBEXT) \
4546
../json/json$(LIBEXT)
4647

src/cbmc/cbmc_languages.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ Author: Daniel Kroening, [email protected]
1616
#include <ansi-c/ansi_c_language.h>
1717
#include <cpp/cpp_language.h>
1818
#include <json-symtab-language/json_symtab_language.h>
19+
#include <structured-text/structured_text_language.h>
1920

2021
#ifdef HAVE_JSIL
21-
#include <jsil/jsil_language.h>
22+
# include <jsil/jsil_language.h>
2223
#endif
2324

2425
void cbmc_parse_optionst::register_languages()
2526
{
2627
register_language(new_ansi_c_language);
28+
register_language(new_structured_text_language);
2729
register_language(new_cpp_language);
2830
register_language(new_json_symtab_language);
2931

src/cbmc/module_dependencies.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ langapi # should go away
1313
linking
1414
pointer-analysis
1515
solvers
16+
structured-text
1617
xmllang
1718
util

src/structured-text/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
generic_bison(structured_text)
2+
generic_flex(structured_text)
3+
4+
################################################################################
5+
6+
7+
file(GLOB_RECURSE sources "*.cpp" "*.h")
8+
9+
add_library(structured-text
10+
${sources}
11+
${BISON_parser_OUTPUTS}
12+
${FLEX_scanner_OUTPUTS}
13+
)
14+
15+
set_source_files_properties(
16+
${sources}
17+
PROPERTIES
18+
OBJECT_DEPENDS "${extra_dependencies}"
19+
)
20+
21+
generic_includes(structured-text)
22+
23+
target_link_libraries(structured-text util langapi linking)

src/structured-text/Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
SRC = converters/convert_int_value.cpp \
2+
converters/convert_real_value.cpp \
3+
converters/convert_string_value.cpp \
4+
converters/expr2structured_text.cpp \
5+
structured_text_language.cpp \
6+
structured_text_lex.yy.cpp \
7+
structured_text_parse_tree.cpp \
8+
structured_text_parser.cpp \
9+
structured_text_typecheck.cpp \
10+
structured_text_y.tab.cpp \
11+
# Empty last line
12+
13+
INCLUDES= -I ..
14+
15+
include ../config.inc
16+
include ../common
17+
18+
CLEANFILES = stl$(LIBEXT) \
19+
structured_text_y.tab.h structured_text_y.tab.cpp \
20+
structured_text_lex.yy.cpp structured_text_y.tab.cpp.output \
21+
structured_text_y.output
22+
23+
all: structured-text$(LIBEXT)
24+
25+
###############################################################################
26+
27+
structured_text_y.tab.cpp: parser.y
28+
$(YACC) $(YFLAGS) $$flags -pyystructured_text -d parser.y -o $@
29+
30+
structured_text_y.tab.h: structured_text_y.tab.cpp
31+
if [ -e structured_text_y.tab.hpp ] ; then mv structured_text_y.tab.hpp \
32+
structured_text_y.tab.h ; else mv structured_text_y.tab.cpp.h \
33+
structured_text_y.tab.h ; fi
34+
35+
structured_text_lex.yy.cpp: scanner.l
36+
$(LEX) -Pyystructured_text -o$@ scanner.l
37+
38+
# extra dependencies
39+
structured_text_y.tab$(OBJEXT): structured_text_y.tab.cpp \
40+
structured_text_y.tab.h
41+
structured_text_lex.yy$(OBJEXT): structured_text_y.tab.cpp \
42+
structured_text_lex.yy.cpp structured_text_y.tab.h
43+
44+
###############################################################################
45+
46+
generated_files: structured_text_y.tab.cpp structured_text_lex.yy.cpp \
47+
structured_text_y.tab.h
48+
49+
###############################################################################
50+
51+
structured-text$(LIBEXT): $(OBJ)
52+
$(LINKLIB)
53+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************\
2+
3+
Module: Conversion from Expression or Type to Structured Text
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "expr2structured_text.h"
10+
#include <util/symbol.h>
11+
12+
std::string expr2stl(const exprt &expr)
13+
{
14+
// TODO: Implement.
15+
return "";
16+
}
17+
18+
std::string type2stl(const typet &type)
19+
{
20+
// TODO: Implement.
21+
return "";
22+
}
23+
24+
std::string type2stl(const typet &type, const std::string &identifier)
25+
{
26+
// TODO: Implement.
27+
return "";
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************\
2+
3+
Module: Conversion from Expression or Type to Structured Text
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_STRUCTURED_TEXT_CONVERTERS_EXPR2STRUCTURED_TEXT_H
10+
#define CPROVER_STRUCTURED_TEXT_CONVERTERS_EXPR2STRUCTURED_TEXT_H
11+
12+
#include <string>
13+
14+
/// Converts a given expression to human-readable STL code.
15+
/// \param expr: Expression to be converted.
16+
/// \param identifier: Identifier to be converted.
17+
/// \result String with the STL representation of the given parameters.
18+
std::string expr2stl(const class exprt &expr);
19+
20+
/// Converts a given type to human-readable STL code.
21+
/// \param type: Type to be converted.
22+
/// \result String with the STL representation of the given type.
23+
std::string type2stl(const class typet &type);
24+
25+
/// Converts a given type and identifier to human-readable STL code.
26+
/// \param type: Type to be converted.
27+
/// \param identifier: Identifier to be converted.
28+
/// \result String with the STL representation of the given parameters.
29+
std::string type2stl(const typet &type, const std::string &identifier);
30+
31+
#endif // CPROVER_STRUCTURED_TEXT_CONVERTERS_EXPR2STRUCTURED_TEXT_H
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*******************************************************************\
2+
3+
Module: Structured Text Language Interface
4+
5+
Author: Matthias Weiss, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Structured Text Language Interface
11+
12+
#include "structured_text_language.h"
13+
#include "converters/expr2structured_text.h"
14+
#include "structured_text_parser.h"
15+
#include "structured_text_typecheck.h"
16+
17+
#include <linking/linking.h>
18+
#include <linking/remove_internal_symbols.h>
19+
#include <util/get_base_name.h>
20+
21+
void structured_text_languaget::set_language_options(const optionst &options)
22+
{
23+
(void)options;
24+
return;
25+
}
26+
27+
bool structured_text_languaget::generate_support_functions(
28+
symbol_tablet &symbol_table)
29+
{
30+
return false;
31+
}
32+
33+
bool structured_text_languaget::typecheck(
34+
symbol_tablet &symbol_table,
35+
const std::string &module,
36+
const bool keep_file_local)
37+
{
38+
symbol_tablet new_symbol_table;
39+
40+
if(structured_text_typecheck(
41+
parse_tree, new_symbol_table, module, get_message_handler()))
42+
{
43+
return true;
44+
}
45+
46+
remove_internal_symbols(
47+
new_symbol_table, this->get_message_handler(), keep_file_local);
48+
49+
if(linking(symbol_table, new_symbol_table, get_message_handler()))
50+
return true;
51+
52+
return false;
53+
}
54+
55+
bool structured_text_languaget::parse(
56+
std::istream &instream,
57+
const std::string &path)
58+
{
59+
structured_text_parser.clear();
60+
parse_path = path;
61+
structured_text_parser.set_line_no(0);
62+
structured_text_parser.set_file(path);
63+
structured_text_parser.in = &instream;
64+
structured_text_scanner_init();
65+
bool result = structured_text_parser.parse();
66+
67+
// store result
68+
structured_text_parser.swap_tree(parse_tree);
69+
70+
return result;
71+
}
72+
73+
void structured_text_languaget::show_parse(std::ostream &out)
74+
{
75+
parse_tree.output(out);
76+
}
77+
78+
bool structured_text_languaget::can_keep_file_local()
79+
{
80+
return true;
81+
}
82+
83+
bool structured_text_languaget::typecheck(
84+
symbol_tablet &symbol_table,
85+
const std::string &module)
86+
{
87+
return typecheck(symbol_table, module, true);
88+
}
89+
90+
bool structured_text_languaget::from_expr(
91+
const exprt &expr,
92+
std::string &code,
93+
const namespacet &ns)
94+
{
95+
code = expr2stl(expr);
96+
return false;
97+
}
98+
99+
bool structured_text_languaget::from_type(
100+
const typet &type,
101+
std::string &code,
102+
const namespacet &ns)
103+
{
104+
code = type2stl(type);
105+
return false;
106+
}
107+
108+
bool structured_text_languaget::type_to_name(
109+
const typet &type,
110+
std::string &name,
111+
const namespacet &ns)
112+
{
113+
return from_type(type, name, ns);
114+
}
115+
116+
bool structured_text_languaget::to_expr(
117+
const std::string &code,
118+
const std::string &module,
119+
exprt &expr,
120+
const namespacet &ns)
121+
{
122+
// unused parameters
123+
(void)code;
124+
(void)module;
125+
(void)expr;
126+
(void)ns;
127+
128+
return true;
129+
}
130+
131+
structured_text_languaget::structured_text_languaget()
132+
{
133+
}
134+
135+
structured_text_languaget::~structured_text_languaget()
136+
{
137+
parse_tree.clear();
138+
}
139+
140+
void structured_text_languaget::modules_provided(std::set<std::string> &modules)
141+
{
142+
modules.insert(get_base_name(parse_path, true));
143+
}
144+
145+
std::set<std::string> structured_text_languaget::extensions() const
146+
{
147+
return {"awl"};
148+
}
149+
150+
std::unique_ptr<languaget> new_structured_text_language()
151+
{
152+
return util_make_unique<structured_text_languaget>();
153+
}
154+
155+
std::unique_ptr<languaget> structured_text_languaget::new_language()
156+
{
157+
return util_make_unique<structured_text_languaget>();
158+
}
159+
160+
std::string structured_text_languaget::id() const
161+
{
162+
return "Structured Text";
163+
}
164+
165+
std::string structured_text_languaget::description() const
166+
{
167+
return "Structured Text Language by Siemens";
168+
}

0 commit comments

Comments
 (0)