Skip to content

Commit

Permalink
Compiladores: Bloco 1 poorly done
Browse files Browse the repository at this point in the history
  • Loading branch information
JCapucho committed Mar 4, 2024
1 parent 8c0e36e commit 8ec4570
Show file tree
Hide file tree
Showing 20 changed files with 1,932 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2ano2/Compiladores/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
11 changes: 11 additions & 0 deletions 2ano2/Compiladores/Compiladores.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
60 changes: 60 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2017-2021

scriptName="$0"
target="java"
options=""

function help()
{
echo "Usage: $scriptName [-h | -help | -cpp | -python | -java] ..."
echo ""
echo " Compiles an antlr4 grammar to a target language."
echo ""
echo " Target language:"
echo " -java: java (default)"
echo " -cpp: c++"
echo " -python: python3"
#exit 1
}

case $1 in
-h | -help)
help
echo ""
shift
;;
-cpp | -c++)
target="cpp"
options="-Dlanguage=Cpp"
shift
;;
-python | -python3)
target="python3"
options="-Dlanguage=Python3"
shift
;;
-java)
shift
;;
esac

if [ $# -eq 0 ]; then
options=""
fi

if [ -z "$ANTLR4_PATH" ]; then
java -ea org.antlr.v4.Tool $options $*
else
TMP_VERSION=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | tail -1 | sed 's/.*\///g' | sed 's/-com.*//g')
ANTLR4File=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
TMP_VERSION=$(ls -f $ANTLR4_PATH/ST-*.jar | tail -1 | sed 's/.*\///g' | sed 's/\.jar//g')
STFile=$(ls -f $ANTLR4_PATH/ST-*.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)

#echo "java -ea -cp .:$ANTLR4File:$STFile org.antlr.v4.Tool $options $*"
java -ea -cp .:"$ANTLR4File":"$STFile" org.antlr.v4.Tool $options $*
fi

exit $?

97 changes: 97 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2017-2024

IFS=$'\n'

scriptName="$0"
target="java"

function help()
{
echo "Usage: $scriptName [-h | -help | -cpp | -python | -java] [grammar]"
echo ""
echo " Finds all existing *.g4 grammars in current directory or in subdirectories and,"
echo " if no grammar argument exists, and if successful and if applicable, compiles the"
echo " generated target language code."
echo ""
echo " target language:"
echo " -java: java (default)"
echo " -cpp: c++"
echo " -python: python3"
echo ""
echo " grammar argument is the name of the grammar (if present, extension .g4 is ignored)."

exit 1
}

function process()
{
g="$1"
echo "Processing $g"
if [ -e ${g}.g4 ]; then
grammar=${g}.g4
elif [ -e ${g}Parser.g4 ]; then
grammar=${g}Parser.g4
else
echo "ERROR: internal error!"
exit -1
fi
case $target in
java)
(cd `dirname $g`; if ( grep Visitor *java &> /dev/null ); then opt="-visitor"; else opt=""; fi; antlr4 $opt `basename $g`*.g4) && javac ./`dirname $g`/*.java
;;
cpp)
(cd `dirname $g`; make)
;;
python)
parserFile=${g}Parser.py
if [[ ! -e ${parserFile} || ${grammar} -nt ${parserFile} ]]; then
(cd `dirname $g`; if ( grep Visitor *py &> /dev/null ); then opt="-visitor"; else opt=""; fi; antlr4 -python $opt `basename $g`*.g4)
fi
;;
esac
}

case $1 in
"")
;;
-h | -help)
help
;;
-java)
shift
;;
-cpp | -c++)
shift
target="cpp"
;;
-python | -python3)
shift
target="python"
;;
-*)
echo "ERROR: invalid argument!"
echo ""
help
exit 1
;;
esac

if ! [ -z $1 ]; then
target_grammar=${1/.g4/}
if ! [ -f ${target_grammar}.g4 ]; then
if ! [ -f ${target_grammar}Parser.g4 ]; then
echo "ERROR: grammar file \"${target_grammar}.g4\" does not exist!"
exit 2
fi
fi
process $target_grammar
exit 0
fi

for g in `find . -name \*.g4 | sed 's/Parser.g4$\|Lexer.g4$\|.g4$//1' | uniq | sort`; do
process $g
done

exit 0
112 changes: 112 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4-clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2017-2024

IFS=$'\n'

scriptName="$0"
target="java"
sourceExt="java"
sourceExt2=""
objExt="class"

function help()
{
echo "Usage: $scriptName [-h | -help | -cpp | -python | -java] [grammar]"
echo ""
echo " Cleans automatically generated antlr4 files considering target language."
echo ""
echo " target language:"
echo " -java: java (default)"
echo " -cpp: c++"
echo " -python: python3"
echo ""
echo " grammar argument is the name of the grammar (if present, extension .g4 is ignored)."
exit 1
}

function process()
{
if [ -e ${1} ]; then
g="$1"
elif [ -e ${1}.g4 ]; then
g=${1}.g4
elif [ -e ${1}Parser.g4 ]; then
g=${1}.g4
else
echo "ERROR: internal error!"
exit -1
fi
echo "Processing $g"
if [[ -e $g && -f $g ]]; then
echo "Removing $g files:"
name=${g%.g4}
rm -fv $name.tokens $name.interp ${name}Lexer.interp ${name}BaseListener.$sourceExt ${name}Listener.$sourceExt ${name}BaseVisitor.$sourceExt ${name}Visitor.$sourceExt ${name}Lexer.tokens ${name}Lexer.$sourceExt ${name}Parser.$sourceExt
if [ ! -z "$objExt" ]; then
rm -fv ${name}BaseListener.$sourceExt2 ${name}Listener.$sourceExt2 ${name}BaseVisitor.$sourceExt2 ${name}Visitor.$sourceExt2 ${name}Lexer.$sourceExt2 ${name}Parser.$sourceExt2
fi
if [[ "$name" = *Parser || "$name" = *Lexer ]]; then
rm -fv $name.$sourceExt
fi
if [[ "$target" = "cpp" && -e ${name}Main ]]; then
rm -fv ${name}Main
fi
if [[ "$target" = "python" ]]; then
cacheDir=`dirname $g`/__pycache__
[[ -e $cacheDir ]] && rm -rfv `dirname $g`/__pycache__
fi
fi
}

case $1 in
"")
;;
-h | -help)
help
;;
-cpp | -c++)
target="cpp"
sourceExt="cpp"
sourceExt2="h"
objExt="o"
shift
;;
-python | -python3)
target="python"
sourceExt="py"
objExt=""
shift
;;
-java)
shift
;;
-*)
echo "ERROR: invalid argument!"
echo ""
help
;;
esac

if [ ! -z "$objExt" ]; then
# clean objExt files:
echo "Removing *.$objExt files:"
find . -name \*.$objExt -printf "removing %p\n" -exec rm -f {} \;
fi

if ! [ -z $1 ]; then
target_grammar=${1/.g4/}
if ! [ -f ${target_grammar}.g4 ]; then
if ! [ -f ${target_grammar}Parser.g4 ]; then
echo "ERROR: grammar file \"${target_grammar}.g4\" does not exist!"
exit 2
fi
fi
process $target_grammar
exit 0
fi

# clean antlr4 generated .java files:
for g in `find . -name \*.g4`; do
process $g
done

35 changes: 35 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4-jar-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2020

if (( $# == 0 )); then
echo "USAGE: antlr4-jar-run <executable-jar-file>"
exit 1
fi

jarFile=$1
shift
if ! [ -e ${jarFile} ]; then
echo "ERROR: jar file ${jarFile} not found!"
exit 2
fi
mainClass=$(unzip -q -c $jarFile META-INF/MANIFEST.MF | grep Main-Class | cut -d":" -f2 | sed 's/[ \n\r]//g')

if [ -z ${mainClass} ]; then
echo "ERROR: jar file ${jarFile} without main class!"
exit 3
fi

if [ -z "$ANTLR4_PATH" ]; then
cp="-cp ${jarFile}"
else
TMP_VERSION=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | tail -1 | sed 's/.*\///g' | sed 's/-com.*//g')
ANTLR4File=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
TMP_VERSION=$(ls -f $ANTLR4_PATH/ST-*.jar | tail -1 | sed 's/.*\///g' | sed 's/\.jar//g')
STFile=$(ls -f $ANTLR4_PATH/ST-*.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
cp="-cp ${jarFile}:$ANTLR4File:$STFile"
fi

java -ea $cp $mainClass $*

exit $?
17 changes: 17 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4-java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2020

if [ -z "$ANTLR4_PATH" ]; then
cp=""
else
TMP_VERSION=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | tail -1 | sed 's/.*\///g' | sed 's/-com.*//g')
ANTLR4File=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
TMP_VERSION=$(ls -f $ANTLR4_PATH/ST-*.jar | tail -1 | sed 's/.*\///g' | sed 's/\.jar//g')
STFile=$(ls -f $ANTLR4_PATH/ST-*.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
cp="-cp .:$ANTLR4File:$STFile"
fi

java -ea $cp $*

exit $?
17 changes: 17 additions & 0 deletions 2ano2/Compiladores/antlr4-bin/antlr4-javac
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
#
# Miguel Oliveira e Silva ([email protected]), 2020

if [ -z "$ANTLR4_PATH" ]; then
cp=""
else
TMP_VERSION=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | tail -1 | sed 's/.*\///g' | sed 's/-com.*//g')
ANTLR4File=$(ls -f $ANTLR4_PATH/antlr-4*-complete.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
TMP_VERSION=$(ls -f $ANTLR4_PATH/ST-*.jar | tail -1 | sed 's/.*\///g' | sed 's/\.jar//g')
STFile=$(ls -f $ANTLR4_PATH/ST-*.jar | grep "$TMP_VERSION" | awk '{ print length(), $0 | "sort -n" }' | sed 's/^[0-9 ]\+//g' | tail -1)
cp="-cp .:$ANTLR4File:$STFile"
fi

javac $cp $*

exit $?
Loading

0 comments on commit 8ec4570

Please sign in to comment.