Add CContext to store context information while parsing

* For now, it is being used to denote whether to use a semicolon or
      not
This commit is contained in:
Dhruv Maroo
2023-04-26 08:41:30 +05:30
parent f0f3d0d747
commit 2891356e1b
10 changed files with 507 additions and 494 deletions

View File

@@ -0,0 +1,9 @@
package ghidrust.decompiler.parser.c;
public class CContext {
public boolean statement_end_sc;
public CContext() {
statement_end_sc = true;
}
}

View File

@@ -4,8 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ /* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public public
class ASTGhostStringToken extends SimpleNode { class ASTGhostStringToken extends SimpleNode {
String str_val; public String image;
public ASTGhostStringToken(int id) { public ASTGhostStringToken(int id) {
super(id); super(id);
} }
@@ -21,13 +20,5 @@ class ASTGhostStringToken extends SimpleNode {
return return
visitor.visit(this, data); visitor.visit(this, data);
} }
public String getValue() {
return str_val;
} }
/* JavaCC - OriginalChecksum=f69f4334f2315a848a78628a0a52e742 (do not edit this line) */
public void setValue(String value) {
this.str_val = value;
}
}
/* JavaCC - OriginalChecksum=7d91f560265b12b4f437803bcd66b7ba (do not edit this line) */

View File

@@ -4,6 +4,8 @@ package ghidrust.decompiler.parser.c.gen;
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ /* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public public
class ASTIterationStatement extends SimpleNode { class ASTIterationStatement extends SimpleNode {
public int choice;
public ASTIterationStatement(int id) { public ASTIterationStatement(int id) {
super(id); super(id);
} }

View File

@@ -4,7 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ /* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public public
class ASTStringToken extends SimpleNode { class ASTStringToken extends SimpleNode {
String str_val; public String image;
public ASTStringToken(int id) { public ASTStringToken(int id) {
super(id); super(id);
@@ -21,13 +21,5 @@ class ASTStringToken extends SimpleNode {
return return
visitor.visit(this, data); visitor.visit(this, data);
} }
public String getValue() {
return str_val;
} }
/* JavaCC - OriginalChecksum=73f64d714d8c3f0f819df770be5c932d (do not edit this line) */
public void setValue(String value) {
this.str_val = value;
}
}
/* JavaCC - OriginalChecksum=c289df07a5b51163b866d4bfab28fb00 (do not edit this line) */

View File

@@ -4,7 +4,7 @@ package ghidrust.decompiler.parser.c.gen;
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ /* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public public
class ASTTypeStringToken extends SimpleNode { class ASTTypeStringToken extends SimpleNode {
String str_val; public String image;
public ASTTypeStringToken(int id) { public ASTTypeStringToken(int id) {
super(id); super(id);
@@ -21,13 +21,5 @@ class ASTTypeStringToken extends SimpleNode {
return return
visitor.visit(this, data); visitor.visit(this, data);
} }
public String getValue() {
return str_val;
} }
/* JavaCC - OriginalChecksum=b189beab6a69b3fbf2cd6d3dc65da871 (do not edit this line) */
public void setValue(String value) {
this.str_val = value;
}
}
/* JavaCC - OriginalChecksum=726f5c0cafc2cba1f5ca608dfd54d3e2 (do not edit this line) */

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,7 @@ import java.io.InputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import ghidrust.decompiler.parser.c.CVisitor; import ghidrust.decompiler.parser.c.CVisitor;
import ghidrust.decompiler.parser.c.CContext;
/** Token Manager. */ /** Token Manager. */
@SuppressWarnings ("unused") @SuppressWarnings ("unused")

View File

@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import ghidrust.decompiler.parser.c.CVisitor; import ghidrust.decompiler.parser.c.CVisitor;
import ghidrust.decompiler.parser.c.CContext;
public class CParser/*@bgen(jjtree)*/implements CParserTreeConstants/*@egen*/ {/*@bgen(jjtree)*/ public class CParser/*@bgen(jjtree)*/implements CParserTreeConstants/*@egen*/ {/*@bgen(jjtree)*/
protected JJTCParserState jjtree = new JJTCParserState(); protected JJTCParserState jjtree = new JJTCParserState();
@@ -48,7 +49,7 @@ public class CParser/*@bgen(jjtree)*/implements CParserTreeConstants/*@egen*/ {/
public static String parse() throws ParseException { public static String parse() throws ParseException {
CParserVisitor visitor = new CVisitor(); CParserVisitor visitor = new CVisitor();
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, null); return (String) c_parser.FunctionDefinition().jjtAccept(visitor, new CContext());
} }
} }
@@ -293,7 +294,7 @@ void StorageClassSpecifier() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {
@@ -322,7 +323,7 @@ void TypeSpecifier() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {
@@ -350,7 +351,7 @@ void TypeQualifier() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {
@@ -1065,17 +1066,28 @@ void SelectionStatement() : {/*@bgen(jjtree) SelectionStatement */
/*@egen*/ /*@egen*/
} }
void IterationStatement() : {/*@bgen(jjtree) IterationStatement */ void IterationStatement() :
{/*@bgen(jjtree) IterationStatement */
ASTIterationStatement jjtn000 = new ASTIterationStatement(JJTITERATIONSTATEMENT); ASTIterationStatement jjtn000 = new ASTIterationStatement(JJTITERATIONSTATEMENT);
boolean jjtc000 = true; boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000); jjtree.openNodeScope(jjtn000);
/*@egen*/} /*@egen*/
int choice = 0;
}
{/*@bgen(jjtree) IterationStatement */ {/*@bgen(jjtree) IterationStatement */
try { try {
/*@egen*/ /*@egen*/
( <WHILE> "(" Expression() ")" Statement() | ( <WHILE> "(" Expression() ")" Statement() { choice = 1; } |
<DO> Statement() <WHILE> "(" Expression() ")" ";" | <DO> Statement() <WHILE> "(" Expression() ")" ";" { choice = 2; } |
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() )/*@bgen(jjtree)*/ <FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() { choice = 3; } )/*@bgen(jjtree)*/
{
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
}
/*@egen*/
{
jjtn000.choice = choice;
}/*@bgen(jjtree)*/
} catch (Throwable jjte000) { } catch (Throwable jjte000) {
if (jjtc000) { if (jjtc000) {
jjtree.clearNodeScope(jjtn000); jjtree.clearNodeScope(jjtn000);
@@ -1213,7 +1225,7 @@ void AssignmentOperator() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {
@@ -1582,7 +1594,7 @@ void AdditionOperator() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {
@@ -1826,22 +1838,22 @@ ASTStringToken Identifier() :
Token r = null; Token r = null;
ASTStringToken t = null; ASTStringToken t = null;
Token s = null; Token s = null;
int choice; int choice = 0;
} }
{/*@bgen(jjtree) StringToken */ {/*@bgen(jjtree) StringToken */
try { try {
/*@egen*/ /*@egen*/
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 0; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 1; })/*@bgen(jjtree)*/ (LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 1; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 2; })/*@bgen(jjtree)*/
{ {
jjtree.closeNodeScope(jjtn000, true); jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false; jjtc000 = false;
} }
/*@egen*/ /*@egen*/
{ {
if (choice == 1) { if (choice == 2) {
jjtn000.setValue((r != null ? r.image : "") + "<" + t.getValue() + ">" + (s != null ? s.image : "")); jjtn000.image = (r != null ? r.image : "") + "<" + t.image + ">" + (s != null ? s.image : "");
} else { } else if (choice == 1) {
jjtn000.setValue(r.image); jjtn000.image = r.image;
} }
return jjtn000; return jjtn000;
@@ -1886,7 +1898,7 @@ void Constant() :
} }
/*@egen*/ /*@egen*/
{ {
jjtn000.setValue(t.image); jjtn000.image = t.image;
}/*@bgen(jjtree)*/ }/*@bgen(jjtree)*/
} finally { } finally {
if (jjtc000) { if (jjtc000) {

View File

@@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import ghidrust.decompiler.parser.c.CVisitor; import ghidrust.decompiler.parser.c.CVisitor;
import ghidrust.decompiler.parser.c.CContext;
public class CParser { public class CParser {
private static CParser c_parser; private static CParser c_parser;
@@ -44,7 +45,7 @@ public class CParser {
public static String parse() throws ParseException { public static String parse() throws ParseException {
CParserVisitor visitor = new CVisitor(); CParserVisitor visitor = new CVisitor();
return (String) c_parser.FunctionDefinition().jjtAccept(visitor, null); return (String) c_parser.FunctionDefinition().jjtAccept(visitor, new CContext());
} }
} }
@@ -169,7 +170,7 @@ void StorageClassSpecifier() #GhostStringToken :
{ {
( t = <AUTO> | t = <REGISTER> | t = <STATIC> | t = <EXTERN> | t = <TYPEDEF> ) ( t = <AUTO> | t = <REGISTER> | t = <STATIC> | t = <EXTERN> | t = <TYPEDEF> )
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }
@@ -181,7 +182,7 @@ void TypeSpecifier() #TypeStringToken :
( t = <VOID> | t = <CHAR> | t = <SHORT> | t = <INT> | t = <LONG> | t = <FLOAT> | t = <DOUBLE> | t = <SIGNED> | ( t = <VOID> | t = <CHAR> | t = <SHORT> | t = <INT> | t = <LONG> | t = <FLOAT> | t = <DOUBLE> | t = <SIGNED> |
t = <UNSIGNED> | t = <BOOL_TYPE> | t = <CODE> | t = <UNDEFINED_TYPE> ) t = <UNSIGNED> | t = <BOOL_TYPE> | t = <CODE> | t = <UNDEFINED_TYPE> )
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }
@@ -192,7 +193,7 @@ void TypeQualifier() #StringToken :
{ {
( t = <CONST> | t = <VOLATILE> ) ( t = <CONST> | t = <VOLATILE> )
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }
@@ -329,11 +330,17 @@ void SelectionStatement() : {}
<SWITCH> "(" Expression() ")" Statement() ) <SWITCH> "(" Expression() ")" Statement() )
} }
void IterationStatement() : {} void IterationStatement() :
{ {
( <WHILE> "(" Expression() ")" Statement() | int choice = 0;
<DO> Statement() <WHILE> "(" Expression() ")" ";" | }
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() ) {
( <WHILE> "(" Expression() ")" Statement() { choice = 1; } |
<DO> Statement() <WHILE> "(" Expression() ")" ";" { choice = 2; } |
<FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() { choice = 3; } )
{
jjtThis.choice = choice;
}
} }
void JumpStatement() : {} void JumpStatement() : {}
@@ -362,7 +369,7 @@ void AssignmentOperator() #StringToken :
{ {
( t = "=" | t = "*=" | t = "/=" | t = "%=" | t = "+=" | t = "-=" | t = "<<=" | t = ">>=" | t = "&=" | t = "^=" | t = "|=" ) ( t = "=" | t = "*=" | t = "/=" | t = "%=" | t = "+=" | t = "-=" | t = "<<=" | t = ">>=" | t = "&=" | t = "^=" | t = "|=" )
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }
@@ -428,7 +435,7 @@ void AdditionOperator() #StringToken :
{ {
( t = "+" | t = "-" ) ( t = "+" | t = "-" )
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }
@@ -489,15 +496,15 @@ ASTStringToken Identifier() #StringToken :
Token r = null; Token r = null;
ASTStringToken t = null; ASTStringToken t = null;
Token s = null; Token s = null;
int choice; int choice = 0;
} }
{ {
(LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 0; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 1; }) (LOOKAHEAD(<IDENTIFIER>) r = <IDENTIFIER> { choice = 1; } | [ r = <IDENTIFIER> ] "<" t = Identifier() ">" [ s = <IDENTIFIER> ] { choice = 2; })
{ {
if (choice == 1) { if (choice == 2) {
jjtThis.setValue((r != null ? r.image : "") + "<" + t.getValue() + ">" + (s != null ? s.image : "")); jjtThis.image = (r != null ? r.image : "") + "<" + t.image + ">" + (s != null ? s.image : "");
} else { } else if (choice == 1) {
jjtThis.setValue(r.image); jjtThis.image = r.image;
} }
return jjtThis; return jjtThis;
@@ -511,6 +518,6 @@ void Constant() #StringToken :
{ {
(t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> | t = <CHARACTER_LITERAL> | t = <STRING_LITERAL> | t = <BOOLEAN>) (t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> | t = <CHARACTER_LITERAL> | t = <STRING_LITERAL> | t = <BOOLEAN>)
{ {
jjtThis.setValue(t.image); jjtThis.image = t.image;
} }
} }

View File

@@ -4,7 +4,7 @@
cd c/gen cd c/gen
BACKUP_FILES="ASTPostfixExpression \ BACKUP_FILES="ASTPostfixExpression ASTIterationStatement \
$(ls -1 AST*Token.java | cut -d. -f1 | tr '\n' ' ')" $(ls -1 AST*Token.java | cut -d. -f1 | tr '\n' ' ')"
for file in $BACKUP_FILES; do for file in $BACKUP_FILES; do