From e19617a7d37ffeef43bf1c4eef523c7fb1dbabe7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz>
Date: Mon, 3 Feb 2014 16:41:37 +0100
Subject: [PATCH] aconvert.grammar - Human readable grammar notation. Depends
 on Rule.toString() method.

---
 aconvert.grammar/.cproject                | 127 ++++++++++++++++++++++
 aconvert.grammar/.project                 |  27 +++++
 aconvert.grammar/makefile                 |  20 ++++
 aconvert.grammar/src/GrammarPrinter.cpp   |  47 ++++++++
 aconvert.grammar/src/GrammarPrinter.h     |  21 ++++
 aconvert.grammar/src/aconvert.grammar.cpp |  34 ++++++
 6 files changed, 276 insertions(+)
 create mode 100644 aconvert.grammar/.cproject
 create mode 100644 aconvert.grammar/.project
 create mode 100644 aconvert.grammar/makefile
 create mode 100644 aconvert.grammar/src/GrammarPrinter.cpp
 create mode 100644 aconvert.grammar/src/GrammarPrinter.h
 create mode 100644 aconvert.grammar/src/aconvert.grammar.cpp

diff --git a/aconvert.grammar/.cproject b/aconvert.grammar/.cproject
new file mode 100644
index 0000000000..c0b3d1801b
--- /dev/null
+++ b/aconvert.grammar/.cproject
@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
+	<storageModule moduleId="org.eclipse.cdt.core.settings">
+		<cconfiguration id="cdt.managedbuild.config.gnu.exe.debug.64430919">
+			<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.exe.debug.64430919" moduleId="org.eclipse.cdt.core.settings" name="Debug">
+				<externalSettings/>
+				<extensions>
+					<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
+				</extensions>
+			</storageModule>
+			<storageModule moduleId="cdtBuildSystem" version="4.0.0">
+				<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.debug.64430919" name="Debug" parent="cdt.managedbuild.config.gnu.exe.debug">
+					<folderInfo id="cdt.managedbuild.config.gnu.exe.debug.64430919." name="/" resourcePath="">
+						<toolChain id="cdt.managedbuild.toolchain.gnu.exe.debug.1712690034" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.debug">
+							<targetPlatform id="cdt.managedbuild.target.gnu.platform.exe.debug.2101392019" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.debug"/>
+							<builder buildPath="${workspace_loc:/aconvert.grammar}/Debug" id="cdt.managedbuild.target.gnu.builder.exe.debug.1650545282" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.debug"/>
+							<tool id="cdt.managedbuild.tool.gnu.archiver.base.1348825825" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
+							<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.1557767569" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug">
+								<option id="gnu.cpp.compiler.exe.debug.option.optimization.level.1661291497" name="Optimization Level" superClass="gnu.cpp.compiler.exe.debug.option.optimization.level" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
+								<option id="gnu.cpp.compiler.exe.debug.option.debugging.level.1066675648" name="Debug Level" superClass="gnu.cpp.compiler.exe.debug.option.debugging.level" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
+								<option id="gnu.cpp.compiler.option.include.paths.228919629" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath">
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/alib/src}&quot;"/>
+									<listOptionValue builtIn="false" value="/usr/include/libxml2"/>
+								</option>
+								<option id="gnu.cpp.compiler.option.other.other.31259776" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11" valueType="string"/>
+								<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.2079250197" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.1620121719" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug">
+								<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.exe.debug.option.optimization.level.437834220" name="Optimization Level" superClass="gnu.c.compiler.exe.debug.option.optimization.level" valueType="enumerated"/>
+								<option id="gnu.c.compiler.exe.debug.option.debugging.level.473346356" name="Debug Level" superClass="gnu.c.compiler.exe.debug.option.debugging.level" value="gnu.c.debugging.level.max" valueType="enumerated"/>
+								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1575412399" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.c.linker.exe.debug.734179437" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.debug"/>
+							<tool id="cdt.managedbuild.tool.gnu.cpp.linker.exe.debug.1067778812" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.exe.debug">
+								<option id="gnu.cpp.link.option.libs.627858262" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" valueType="libs">
+									<listOptionValue builtIn="false" value="alib"/>
+								</option>
+								<option id="gnu.cpp.link.option.paths.591596312" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" valueType="libPaths">
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/alib/lib}&quot;"/>
+									<listOptionValue builtIn="false" value=""/>
+								</option>
+								<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1424246647" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
+									<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
+									<additionalInput kind="additionalinput" paths="$(LIBS)"/>
+								</inputType>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.assembler.exe.debug.1611777659" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.debug">
+								<inputType id="cdt.managedbuild.tool.gnu.assembler.input.376143327" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
+							</tool>
+						</toolChain>
+					</folderInfo>
+				</configuration>
+			</storageModule>
+			<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
+		</cconfiguration>
+		<cconfiguration id="cdt.managedbuild.config.gnu.exe.release.1121361280">
+			<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.exe.release.1121361280" moduleId="org.eclipse.cdt.core.settings" name="Release">
+				<externalSettings/>
+				<extensions>
+					<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+					<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
+				</extensions>
+			</storageModule>
+			<storageModule moduleId="cdtBuildSystem" version="4.0.0">
+				<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1121361280" name="Release" parent="cdt.managedbuild.config.gnu.exe.release">
+					<folderInfo id="cdt.managedbuild.config.gnu.exe.release.1121361280." name="/" resourcePath="">
+						<toolChain id="cdt.managedbuild.toolchain.gnu.exe.release.970006224" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.release">
+							<targetPlatform id="cdt.managedbuild.target.gnu.platform.exe.release.45274759" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.release"/>
+							<builder buildPath="${workspace_loc:/aconvert.grammar}/Release" id="cdt.managedbuild.target.gnu.builder.exe.release.263112285" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.release"/>
+							<tool id="cdt.managedbuild.tool.gnu.archiver.base.2117461476" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
+							<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.478659920" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release">
+								<option id="gnu.cpp.compiler.exe.release.option.optimization.level.1407098437" name="Optimization Level" superClass="gnu.cpp.compiler.exe.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
+								<option id="gnu.cpp.compiler.exe.release.option.debugging.level.1198215692" name="Debug Level" superClass="gnu.cpp.compiler.exe.release.option.debugging.level" value="gnu.cpp.compiler.debugging.level.none" valueType="enumerated"/>
+								<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1899627218" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.release.941020788" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.release">
+								<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.exe.release.option.optimization.level.1475736321" name="Optimization Level" superClass="gnu.c.compiler.exe.release.option.optimization.level" valueType="enumerated"/>
+								<option id="gnu.c.compiler.exe.release.option.debugging.level.496505839" name="Debug Level" superClass="gnu.c.compiler.exe.release.option.debugging.level" value="gnu.c.debugging.level.none" valueType="enumerated"/>
+								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.97518528" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.c.linker.exe.release.2003585585" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.release"/>
+							<tool id="cdt.managedbuild.tool.gnu.cpp.linker.exe.release.1240264067" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.exe.release">
+								<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.381637236" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
+									<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
+									<additionalInput kind="additionalinput" paths="$(LIBS)"/>
+								</inputType>
+							</tool>
+							<tool id="cdt.managedbuild.tool.gnu.assembler.exe.release.1129175057" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.release">
+								<inputType id="cdt.managedbuild.tool.gnu.assembler.input.353790346" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
+							</tool>
+						</toolChain>
+					</folderInfo>
+				</configuration>
+			</storageModule>
+			<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
+		</cconfiguration>
+	</storageModule>
+	<storageModule moduleId="cdtBuildSystem" version="4.0.0">
+		<project id="aconvert.grammar.cdt.managedbuild.target.gnu.exe.1030518788" name="Executable" projectType="cdt.managedbuild.target.gnu.exe"/>
+	</storageModule>
+	<storageModule moduleId="scannerConfiguration">
+		<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+		<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.release.1121361280;cdt.managedbuild.config.gnu.exe.release.1121361280.;cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.478659920;cdt.managedbuild.tool.gnu.cpp.compiler.input.1899627218">
+			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+		</scannerConfigBuildInfo>
+		<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.64430919;cdt.managedbuild.config.gnu.exe.debug.64430919.;cdt.managedbuild.tool.gnu.c.compiler.exe.debug.1620121719;cdt.managedbuild.tool.gnu.c.compiler.input.1575412399">
+			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+		</scannerConfigBuildInfo>
+		<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.64430919;cdt.managedbuild.config.gnu.exe.debug.64430919.;cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.1557767569;cdt.managedbuild.tool.gnu.cpp.compiler.input.2079250197">
+			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+		</scannerConfigBuildInfo>
+		<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.release.1121361280;cdt.managedbuild.config.gnu.exe.release.1121361280.;cdt.managedbuild.tool.gnu.c.compiler.exe.release.941020788;cdt.managedbuild.tool.gnu.c.compiler.input.97518528">
+			<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+		</scannerConfigBuildInfo>
+	</storageModule>
+	<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
+	<storageModule moduleId="refreshScope"/>
+</cproject>
diff --git a/aconvert.grammar/.project b/aconvert.grammar/.project
new file mode 100644
index 0000000000..3276fdeabb
--- /dev/null
+++ b/aconvert.grammar/.project
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>aconvert.grammar</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
+			<triggers>clean,full,incremental,</triggers>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
+			<triggers>full,incremental,</triggers>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.cdt.core.cnature</nature>
+		<nature>org.eclipse.cdt.core.ccnature</nature>
+		<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
+		<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+	</natures>
+</projectDescription>
diff --git a/aconvert.grammar/makefile b/aconvert.grammar/makefile
new file mode 100644
index 0000000000..80d67da8a0
--- /dev/null
+++ b/aconvert.grammar/makefile
@@ -0,0 +1,20 @@
+CC=g++
+EXECUTIBLE=aconvert.grammar
+CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib/src -I/usr/include/libxml2
+LDFLAGS= -L../alib/lib -lxml2 -lalib -Wl,-rpath,.
+
+SOURCES=$(shell find src/ -name *cpp)
+OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES))
+
+all: $(SOURCES) bin/$(EXECUTIBLE)
+
+bin/$(EXECUTIBLE): $(OBJECTS)
+	mkdir -p bin
+	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
+
+obj/%.o: src/%.cpp
+	mkdir -p $(dir $@)
+	$(CC) $(CCFLAGS) $< -o $@
+
+clean:
+	$(RM) -r *.o *.d bin obj
diff --git a/aconvert.grammar/src/GrammarPrinter.cpp b/aconvert.grammar/src/GrammarPrinter.cpp
new file mode 100644
index 0000000000..3bfc672e3f
--- /dev/null
+++ b/aconvert.grammar/src/GrammarPrinter.cpp
@@ -0,0 +1,47 @@
+#include "GrammarPrinter.h"
+
+using namespace std;
+using namespace grammar;
+
+GrammarPrinter::GrammarPrinter( std::ostream& out ) : m_Out( out )
+{
+
+}
+
+void GrammarPrinter::print( const grammar::Grammar & grammar )
+{
+    // group by left side
+    map< list<Symbol>, set<Rule> > group;
+    for( const auto & r : grammar.getRules( ) )
+    {
+        if( group.find( r.getLeftSide( ) ) != group.end( ) )
+            group[ r.getLeftSide( ) ].insert( r );
+        else
+            group.insert( std::pair< list<Symbol>, set<Rule> >( r.getLeftSide( ), set<Rule>( ) = { r } ) );
+    }
+
+    m_Out << "G = (N,T,P,S)" << endl;
+
+    auto last = grammar.getNonTerminalSymbols( ).end( );
+    last --;
+    m_Out << "N = {";
+    for( auto it = grammar.getNonTerminalSymbols( ).begin( ) ; it != grammar.getNonTerminalSymbols( ).end( ) ; it++ )
+        m_Out << it->getSymbol( ) << ( it != last ? ", " : "" );
+    m_Out << "}" << endl;
+
+    last = grammar.getTerminalSymbols( ).end( );
+    last --;
+    m_Out << "T = {";
+    for( auto it = grammar.getTerminalSymbols( ).begin( ) ; it != grammar.getTerminalSymbols( ).end( ) ; it++ )
+        m_Out << it->getSymbol( ) << ( it != last ? ", " : "" );
+    m_Out << "}" << endl;
+
+    m_Out << "P = {" << endl;
+    for( const auto & g : group )
+        for( const auto & r : g.second )
+            m_Out << "\t" << r.toString( ) << endl;
+    m_Out << "}" << endl;
+
+    m_Out << "S = " << grammar.getStartSymbol( ).getSymbol( ) << endl;
+}
+
diff --git a/aconvert.grammar/src/GrammarPrinter.h b/aconvert.grammar/src/GrammarPrinter.h
new file mode 100644
index 0000000000..725456c98e
--- /dev/null
+++ b/aconvert.grammar/src/GrammarPrinter.h
@@ -0,0 +1,21 @@
+#ifndef GRAMMAR_PRINTER_H_
+#define GRAMMAR_PRINTER_H_
+
+#include <iostream>
+#include "grammar/Grammar.h"
+#include "grammar/Rule.h"
+
+#include <map>
+#include <list>
+#include <set>
+
+class GrammarPrinter
+{
+  std::ostream& m_Out;
+
+public:
+  GrammarPrinter( std::ostream& out );
+  void print( const grammar::Grammar & grammar );
+};
+
+#endif /* GRAMMAR_PRINTER_H_ */
diff --git a/aconvert.grammar/src/aconvert.grammar.cpp b/aconvert.grammar/src/aconvert.grammar.cpp
new file mode 100644
index 0000000000..633f3d8591
--- /dev/null
+++ b/aconvert.grammar/src/aconvert.grammar.cpp
@@ -0,0 +1,34 @@
+#include <iostream>
+#include <cstdlib>
+
+#include <GrammarFactory.h>
+#include <AlibException.h>
+#include <sax/SaxInterface.h>
+
+#include "GrammarPrinter.h"
+
+
+#define VERSION "0.0.1"
+#define USAGE "aconvert.grammar [inputfile]"
+
+using namespace grammar;
+using namespace sax;
+using namespace alib;
+
+int main(int argc, char** argv)
+{
+    try
+    {
+        string input( istreambuf_iterator<char>( cin ), ( istreambuf_iterator<char>( ) ) );
+
+        UnknownGrammar g = GrammarFactory::fromString( input );
+
+        GrammarPrinter printer( cout );
+        printer.print( g );
+    }
+    catch( AlibException & e )
+    {
+        cout << e.what( ) << endl;
+        return 0;
+    }
+}
-- 
GitLab