Skip to content

Instantly share code, notes, and snippets.

@andersonfraga
Last active August 31, 2015 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfraga/d787178c850fe2640a37 to your computer and use it in GitHub Desktop.
Save andersonfraga/d787178c850fe2640a37 to your computer and use it in GitHub Desktop.
#!/bin/bash
##
# Anderson Jean Fraga
# 13180375
# Compiladores - Trabalho 1 - Execute
##
file="Lexica_0$1"
if [[ -f "$file.flex" ]]; then
ucfile="$(tr '[:lower:]' '[:upper:]' <<< ${file:0:1})${file:1}"
rm $file.{java,java~,html}
jflex $file.flex
javac $ucfile.java
java $ucfile $file.txt
else
echo "File '${file}.flex' not found. Create, too, '${file}.txt' to test"
fi
%%
/**
* Anderson Jean Fraga
* 13180375
* Compiladores - Trabalho 1 - Exercicio 1
*/
%public
%class Lexica_01
%standalone
%ignorecase
%unicode
%line
NEWLINE = \n|\r|\n\r
digit = [0-9]
letter = [a-zA-Z0-9_]
Drive = {letter}
Id = ({letter}|{digit})({letter}|{digit})*
PathName = {Id}
FileName = {Id}
FileType = {Id}
%%
^({Drive}\:)?(\\)?({PathName}\\)*{FileName}(\.{FileType})?$ {
System.out.println("Está ok: " + yytext());
}
{NEWLINE} {}
.* { System.out.println("Está inválido: " + yytext()); }
D:\acad\disc\compil\compil_u02_lexico
ExercicioLexico.doc
\Windows\System32
sa&$\Windows\System32
import java.io.*;
/**
* Anderson Jean Fraga
* 13180375
* Compiladores - Trabalho 1 - Exercicio 2
*/
%%
%init{
try {
writer = new PrintWriter("Lexica_02.html");
writer.append("<html>\n<head>\n");
writer.append("<style>\n");
writer.append("body { background:#23241f; color:#fff; }\n");
writer.append(".reserved { color:#f92672; }\n");
writer.append(".identifiers { color:#66d9ef; }\n");
writer.append(".strings { color:#e6db74; }\n");
writer.append(".numbers { color:#ae81ff; }\n");
writer.append(".comments { color:#75715e; }\n");
writer.append(".operators { color:#a6e22e; }\n");
writer.append(".scopes { color:#7f9f7f; }\n");
writer.append("</style>\n");
writer.append("<body>\n");
}
catch (FileNotFoundException e) {
}
catch (Exception e) {
}
%init}
%eof{
try {
writer.append("</body>\n</html>");
writer.flush();
writer.close();
}
catch (Exception e) {
}
%eof}
%{
private PrintWriter writer = null;
private void toHtml(String klass, String str) {
if (str.length() == 0) {
return;
}
try {
writer.append("<span class=\"" + klass + "\">" + str + "</span>\n");
}
catch (Exception e) {
}
}
private void toHtml(String str) {
if (str.length() == 0) {
return;
}
try {
writer.append("<span>" + str + "</span>\n");
}
catch (Exception e) {
}
}
%}
%public
%class Lexica_02
%standalone
%ignorecase
%byaccj
%integer
%line
%char
%unicode
NEWLINE = \n|\r|\n\r
TAB = \t|\s+
digit = [0-9]
letter = [a-zA-Z0-9_]
letterWithDot = [a-zA-Z0-9\.\*]
%%
=|==|\+|-|\*|\\|\|\||\/|\&\&|\~|\<|\+\+|\>|\<\=|\>\= {
toHtml("operators", yytext());
}
import|final|static|public|protected|private|extends|implements {
toHtml("reserved", yytext());
}
if|else|while|for {
toHtml("reserved", yytext());
}
class|void|String|int|double|float|new|return|null|true|false {
toHtml("identifiers", yytext());
}
\"{letter}+\" {
toHtml("strings", yytext());
}
[-+]?{digit}*(\.{digit}+)? {
toHtml("numbers", yytext());
}
\/\*(.|[\r\n]|\t)*?\*\/ { toHtml("comments", yytext()); }
{letterWithDot}+ { toHtml(yytext()); }
{NEWLINE} { toHtml("<br>"); }
{TAB} { toHtml("&nbsp;&nbsp;&nbsp;&nbsp;"); }
\{|\}|\[|\]|\(|\)|\,|\;|\.|\: { toHtml("scopes", yytext()); }
. { }
import java.utils.*;
public class Teste extends Abc implements Def
{
private String t;
/**
* Construtor
*/
Teste()
{
t = new abc();
}
public void hello()
{
for (int x = 0; x < 5; x++) {
abc.flush();
}
}
private String haa(int as, float sua)
{
if (as == null || sua == null) {
return "putz";
}
else {
abc.hup(as * 3, sua / 12.9);
}
return abc;
}
}
%%
/**
* Anderson Jean Fraga
* 13180375
* Compiladores - Trabalho 1 - Exercicio 3
*/
%public
%class Lexica_03
%standalone
%ignorecase
%unicode
%line
NEWLINE = \t|\n|\r|\n\r
digit = [0-9]
letter = [a-zA-Z0-9_\ ]
%%
\"{letter}+\" {
String itemstr = new String(yytext());
System.out.println("Code: STRING\n - Elem: " + itemstr.substring(1, itemstr.length() - 1) + "\n - Line: " + yyline);
}
"[" {
System.out.println("Code: ABRE_COLCHETE\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
"]" {
System.out.println("Code: FECHA_COLCHETE\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
"{" {
System.out.println("Code: ABRE_CHAVE\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
"}" {
System.out.println("Code: FECHA_CHAVE\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
"," {
System.out.println("Code: VIRGULA\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
":" {
System.out.println("Code: DOIS-PONTOS\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
[-+]?{digit}+ {
System.out.println("Code: INTEIRO\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
[-+]?{digit}*\.{digit}+ {
System.out.println("Code: PONTO FLUTUANTE\n - Elem: " + yytext() + "\n - Line: " + yyline);
}
{NEWLINE}+ { }
. { }
{
"id": 1,
"name": "Toner para Impressora XK 4532",
"price": 219.23,
"tags": [ "Toner", "4532" ],
"stock": {
"shopping iguatemi": 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment