Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
cc285b3
Prefix Convertor, main file, References file, tests added
dhruvgupta22 Jan 31, 2025
371f127
Prefix Convertor, main file, References file, tests ad
dhruvgupta22 Jan 31, 2025
d402f92
constraints parser added
Jan 31, 2025
e4c0616
constraints parser modified for main
Jan 31, 2025
c01e3ec
Add files via upload
kundan9170 Feb 5, 2025
6e3cdec
IrToSmtlib for assignments added
dhruvgupta22 Feb 5, 2025
ac0e7c7
assert statements for assignment
Feb 6, 2025
207cd0b
Docs - Project idea and report1 added
dhruvgupta22 Feb 10, 2025
d5c6be7
README added for submission, references updated
dhruvgupta22 Feb 10, 2025
e994330
If-else statements
Mar 16, 2025
3caba76
If-Else Statements
Mar 16, 2025
5a9aba1
Milestone 1 submission
dhruvgupta22 Mar 16, 2025
51aa175
CFGToSmtlib Debug, Compiled Constraints and smtlib code generation
dhruvgupta22 Mar 16, 2025
02017b0
test cases added for if-else code, regex modified for handling = and ==
Mar 16, 2025
3628e46
minor changes
Mar 16, 2025
14d7c76
Validity of generated code checked, Complete output code generated wi…
dhruvgupta22 Mar 17, 2025
ce67574
smtlib generation for command line params done
Mar 18, 2025
b87cd62
Added support for Unary negation in Infix_To_Prefix
Mar 19, 2025
7eb7569
cube test case added for assignment
Mar 19, 2025
9312694
references added
Mar 19, 2025
0208eae
Minor bug fixes + test case added
dhruvgupta22 Mar 19, 2025
303b460
Minor Bug fixes, Restructured directories
dhruvgupta22 Mar 19, 2025
71090ac
restructuring 2
Mar 19, 2025
e91ccce
Report2 added
dhruvgupta22 Mar 29, 2025
6ab08cb
loops identified
Mar 29, 2025
bd02b62
Loops almost done
dhruvgupta22 Mar 30, 2025
f9afebf
loops partially done
Mar 31, 2025
d9a606a
Assert, Assume, Invariant statements added
dhruvgupta22 Apr 4, 2025
e2fc298
code structure converted to linear IR
dhruvgupta22 Apr 4, 2025
8ed2b57
renamed variables, params code integrated with IR
Apr 4, 2025
220c322
renamed variables, params code integrated with IR
Apr 4, 2025
c85b7dc
smtlib code generated
Apr 4, 2025
d12708e
Mod operator added
Apr 5, 2025
cd2b047
mod operator implemented, forward-backward commands implemented
Apr 5, 2025
cde837e
minor bug fixes
Apr 7, 2025
6bcb00c
REPCOUNTER added for loops
dhruvgupta22 Apr 8, 2025
bb0f560
merged
dhruvgupta22 Apr 8, 2025
2e024dc
REPCOUNTER bug fixed
dhruvgupta22 Apr 8, 2025
e15babf
temp
dhruvgupta22 Apr 12, 2025
ddb3575
temp2
dhruvgupta22 Apr 13, 2025
977fb9b
If-Else-Done
dhruvgupta22 Apr 15, 2025
8255edd
straight line code made compatible
Apr 16, 2025
59265e3
turtle x,y,z,w added in frontend
Apr 16, 2025
388a9b3
Z3 ouput decorated
dhruvgupta22 Apr 16, 2025
7c396cf
Code re-structured
dhruvgupta22 Apr 17, 2025
68d66e9
Merge branch 'master' into master
lahiri-phdworks Apr 17, 2025
1034cad
test cases added
Apr 18, 2025
c7366ed
Merge branch 'master' of https://github.com/dhruvgupta22/ProofEngine
Apr 18, 2025
ad30cc4
more testcases, runs images added
Apr 18, 2025
c1e7fa1
README updated
dhruvgupta22 Apr 19, 2025
8339834
park.tl added
Apr 19, 2025
f77c602
park.tl added
Apr 19, 2025
dafa498
park.png added
Apr 19, 2025
cf3ac97
report added
Apr 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ cython_debug/

# Extra files and hidden folder.
testcases/
build/
build/

virtual/
.vscode/
11 changes: 11 additions & 0 deletions ChironCore/ChironAST/ChironAST.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def __init__(self, condition):
def __str__(self):
return self.cond.__str__()

class AnalysisCommand(Instruction):
def __init__(self, statement, condition):
self.stmt = statement
self.cond = condition
def __str__(self):
return self.stmt + " (" + self.cond.__str__() + ")"

class MoveCommand(Instruction):
def __init__(self, motion, expr):
self.direction = motion
Expand Down Expand Up @@ -126,6 +133,10 @@ class Div(BinArithOp):
def __init__(self, lexpr, rexpr):
super().__init__(lexpr, rexpr, "/")

class Mod(BinArithOp):
def __init__(self, lexpr, rexpr):
super().__init__(lexpr, rexpr, "%")


# --Boolean Expressions-----------------------------------------------

Expand Down
35 changes: 34 additions & 1 deletion ChironCore/ChironAST/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def visitMulExpr(self, ctx:tlangParser.MulExprContext):
return ChironAST.Mult(left, right)
elif ctx.multiplicative().DIV():
return ChironAST.Div(left, right)
elif ctx.multiplicative().MOD():
return ChironAST.Mod(left, right)


# Visit a parse tree produced by tlangParser#parenExpr.
Expand All @@ -97,7 +99,7 @@ def visitParenExpr(self, ctx:tlangParser.ParenExprContext):

def visitCondition(self, ctx:tlangParser.ConditionContext):
if ctx.PENCOND():
return ChironAST.PenStatus();
return ChironAST.PenStatus()

if ctx.NOT():
expr1 = self.visit(ctx.condition(0))
Expand Down Expand Up @@ -172,3 +174,34 @@ def visitMoveCommand(self, ctx:tlangParser.MoveCommandContext):

def visitPenCommand(self, ctx:tlangParser.PenCommandContext):
return [(ChironAST.PenCommand(ctx.getText()), 1)]

def visitAnalysisCommand(self, ctx:tlangParser.AnalysisCommandContext):
analysisCommand = ctx.analysisStatement().getText()
analysisCondition = self.visit(ctx.condition())
return [(ChironAST.AnalysisCommand(analysisCommand, analysisCondition), 1)]

class astGenPassSMTLIB(astGenPass):
def __init__(self):
super().__init__()
self.repeatInstrCount = 0 # keeps count for no of 'repeat' instructions

def visitIfConditional(self, ctx:tlangParser.IfConditionalContext):
condObj = ChironAST.ConditionCommand(self.visit(ctx.condition()))
thenInstrList = self.visit(ctx.strict_ilist())
boolFalse = ChironAST.ConditionCommand(ChironAST.BoolFalse())
elseInstrList = [(boolFalse, 1)]
jumpOverElseBlock = [(ChironAST.ConditionCommand(ChironAST.BoolFalse()), len(elseInstrList) + 1)]
return [(condObj, len(thenInstrList) + 2)] + thenInstrList + jumpOverElseBlock + elseInstrList

def visitMoveCommand(self, ctx):
mvcommand = ctx.moveOp().getText()
mvexpr = self.visit(ctx.expression())
if mvcommand == "forward" or mvcommand == "backward":
return [(ChironAST.MoveCommand(mvcommand, mvexpr), 1), (ChironAST.NoOpCommand(), 1)]
else:
return super().visitMoveCommand(ctx)

def visitGotoCommand(self, ctx):
return [(super().visitGotoCommand(ctx)), (ChironAST.NoOpCommand(), 1)]


Binary file not shown.
Binary file added ChironCore/ProofEngine/Docs/Final_Report.pdf
Binary file not shown.
Binary file added ChironCore/ProofEngine/Docs/Report1.pdf
Binary file not shown.
Binary file added ChironCore/ProofEngine/Docs/Report2.pdf
Binary file not shown.
159 changes: 159 additions & 0 deletions ChironCore/ProofEngine/Infix_To_Prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import ast
import re

"""
This module provides functionality to convert infix expressions into prefix (SMT-LIB) format.
It uses Python's `ast` module to parse expressions and a custom visitor class to traverse
the abstract syntax tree (AST) and generate the corresponding prefix notation.
"""

class PrefixNotationConverter(ast.NodeVisitor):
"""
A custom AST visitor class to convert infix expressions into prefix notation.
"""

def visit_BinOp(self, node):
"""
Handles binary operations (e.g., +, -, *, /).
Converts them into prefix notation (e.g., (+ left right)).
"""
op = self.get_operator(node.op)
left = self.visit(node.left)
right = self.visit(node.right)
return f"({op} {left} {right})"

def visit_Compare(self, node):
"""
Handles comparison operations (e.g., ==, !=, >, <, >=, <=).
Converts them into prefix notation.
Special handling for "!=" to represent it as (not (= left right)).
"""
op = self.get_operator(node.ops[0])
left = self.visit(node.left)
right = self.visit(node.comparators[0])
if op == '!=': # Handle "!=" as (not (= expr1 expr2))
return f"(not (= {left} {right}))"
else:
return f"({op} {left} {right})"

def visit_BoolOp(self, node):
"""
Handles boolean operations (e.g., and, or).
Converts them into prefix notation (e.g., (and expr1 expr2 ...)).
"""
op = self.get_operator(node.op)
values = " ".join(self.visit(value) for value in node.values)
return f"({op} {values})"

def visit_UnaryOp(self, node):
"""
Handles unary operations (e.g., not, -).
Converts them into prefix notation (e.g., (not operand)).
"""
op = self.get_operator(node.op)
operand = self.visit(node.operand)
return f"({op} {operand})"

def visit_Name(self, node):
"""
Handles variable names.
Returns the variable name as is.
"""
return node.id

def visit_Constant(self, node):
"""
Handles constant values (e.g., numbers, strings).
Returns the constant value as a string.
"""
return str(node.value)

def get_operator(self, op):
"""
Maps Python AST operators to their corresponding SMT-LIB operators.
"""
operators = {
ast.Add: '+',
ast.Sub: '-',
ast.Mult: '*',
ast.Div: 'div',
ast.Gt: '>',
ast.Lt: '<',
ast.GtE: '>=',
ast.LtE: '<=',
ast.Eq: '=',
ast.NotEq: '!=',
ast.Mod: 'mod',
ast.And: 'and',
ast.Or: 'or',
ast.Not: '~',
ast.Assign: '=',
ast.USub: '-', # Unary subtraction
}
return operators[type(op)]

def preprocess_expression(expr: str, replace_eq):
"""
Preprocesses the input expression to make it compatible with Python's `ast` module.
- Replaces variable prefixes (e.g., ":var" -> "var").
- Removes spaces.
- Optionally replaces single "=" with "==" for equality checks.

Args:
expr (str): The input infix expression.
replace_eq (bool): Whether to replace "=" with "==" for equality.

Returns:
str: The preprocessed expression.
"""
expr = re.sub(r":(\w+)", r"\1", expr) # Remove ":" prefix from variables
expr = expr.replace(" ", "") # Remove spaces
if replace_eq:
expr = re.sub(r"(?<![<>=!])=(?!=)", "==", expr) # Replace "=" with "==" for equality
return expr

def Construct_AST(expr: str, replace_eq):
"""
Constructs an abstract syntax tree (AST) from the given infix expression.

Args:
expr (str): The input infix expression.
replace_eq (bool): Whether to preprocess the expression to replace "=" with "==".

Returns:
ast.AST: The constructed AST.

Raises:
Exception: If there is a syntax error in the input expression.
"""
expr = preprocess_expression(expr, replace_eq)
# print(expr) # Debugging print for the preprocessed expression
try:
tree = ast.parse(expr, mode='eval') # Parse the expression in evaluation mode
return tree
except SyntaxError as e:
print(expr) # Debugging print for the expression causing the error
raise Exception(f"Syntax Error: {e}")

def Infix_To_Prefix(expr: str, replace_eq=False):
"""
Converts an infix expression into prefix notation.

Args:
expr (str): The input infix expression.
replace_eq (bool): Whether to preprocess the expression to replace "=" with "==".

Returns:
str: The prefix notation of the expression, or None if the input is empty.
"""
if expr:
tree = Construct_AST(expr, replace_eq)
if tree:
converter = PrefixNotationConverter()
expr = converter.visit(tree.body) # Visit the root of the AST
return expr
else:
return None
else:
return None

Loading